Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

White Screen is showing instead of native splash while app opening from push notification #739

Open
hadiarshad6767 opened this issue Oct 9, 2024 · 1 comment

Comments

@hadiarshad6767
Copy link

hadiarshad6767 commented Oct 9, 2024

For IOS, Android & above Android12

Describe the bug
When every i try to open app from a push notification it is showing white screen for 3-5 seconds instead of default native splash screen. It is working fine when i open app directly but through pus notification it is causing error.

Configuration
flutter_native_splash: ^2.4.1

Device (please complete the following information):
Iphone XS max
Simulator 16 PM
android samsung A-71

To Reproduce
Issue is that it is not loading for 1 ms in the app when app open through push notification..

here is code..
Main..

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding); // Show splash
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  Get.put(PushNotificationController());
  Get.put(ProfileController());
  await PushNotificationService().handleMessage(message);
}

void main() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  SharedPreferences pref = await SharedPreferences.getInstance();
  String? uid = pref.getString('uid');
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  AppVersionController appVersionController = Get.put(AppVersionController());
  bool isVersionCompatible = await appVersionController.checkAppVersion();
  Get.put(PushNotificationController());
  Get.put(ProfileController());
  WidgetsBinding.instance.addObserver(AppLifecycleService());
  if (isVersionCompatible) {
    runApp(MyApp(uid: uid, isVersionCompatible: isVersionCompatible));
  } else {
    runApp(MyApp(uid: null, isVersionCompatible: false));
  }
}

class MyApp extends StatelessWidget {
  final String? uid;
  final bool isVersionCompatible;
  static const double _designWidth = 393;
  static const double _designHeight = 852;

  const MyApp(
      {super.key, required this.uid, required this.isVersionCompatible});

  @override
  Widget build(BuildContext context) {
    if (isVersionCompatible) {
      final AuthController authController = Get.put(AuthController());
      Get.put(InternetConnectivityController());

      WidgetsBinding.instance.addPostFrameCallback((_) {
        authController.checkAlreadyLogin();
      });
    } else {
      FlutterNativeSplash.remove();
    }

    return ScreenUtilInit(
        designSize: const Size(_designWidth, _designHeight),
        builder: (context, widget) {
          return GetMaterialApp(
            theme: ThemeData(
              useMaterial3: true,
              primaryColor: AppColors.lightPink,
            ),
            debugShowCheckedModeBanner: false,
            getPages: AppRoutes.appRoutes(),
            initialRoute: !isVersionCompatible
                ? RouteName.app_version_screen
                : uid == null
                    ? RouteName.on_boarding_screen
                    : RouteName.dashboard_screen,
            // home: const AppMaintenanceScreen(),
          );
        });
  }
}
  Future<void> handleMessage(RemoteMessage message) async {
    // Ensure the splash screen is still visible during initialization
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      if (message.data['type'] == 'm') {
        String endUserJson = message.data['endUser'];
        Map<String, dynamic> endUserMap = jsonDecode(endUserJson);

        UserDataModel endUser = UserDataModel.fromMap(endUserMap);

        // Navigate to the chat screen and remove splash after
        Get.toNamed(RouteName.chat_screen,
            arguments: {'endUser': endUser, 'chatRoom': null});
      } else if (message.data['type'] == 'r') {
        // Navigate to notification screen and remove splash after
        Get.toNamed(RouteName.notification_screen);
      }
    });
  }
WhatsApp.Video.2024-10-09.at.10.41.35.mp4

Flutter Doctor report :
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.24.3, on macOS 15.0.1 24A348 darwin-x64, locale
en-PK)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.0)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.1)
[✓] VS Code (version 1.94.0)
[✓] Connected device (4 available)
[✓] Network resources

• No issues found!

@jonbhanson
Copy link
Owner

Is this on Android API 31? This is a known issue in API 31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants