jonbhanson / flutter_native_splash

Automatically generates native code for adding splash screens in Android and iOS. Customize with specific platform, background color and splash image.
https://pub.dev/packages/flutter_native_splash
MIT License
1.36k stars 215 forks source link

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

Closed hadiarshad6767 closed 6 days ago

hadiarshad6767 commented 1 month ago

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);
      }
    });
  }

https://github.com/user-attachments/assets/d1232357-efdd-453e-aca8-77c323984055

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 commented 1 month ago

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

jonbhanson commented 6 days ago

Closed due to inactivity. Feel free to reopen if you are still having trouble.