How or the place to retailer FCM Background notification knowledge in Hive in Flutter iOS?

0
104


  1. Each ios and AOS obtain background notification usually.
  2. In AOS, obtained background notification knowledge could be saved in Hive by means of backgroundHandler.
  3. However in ios BackgroundHander does not work.
  4. If BackgroundHandler will not be supported on iOS, is there one other method?
  5. Is it not potential to avoid wasting background notification knowledge to Hive in iOS?

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp(
    choices: DefaultFirebaseOptions.currentPlatform,
  );
  if (!Hive.isAdapterRegistered(1)) {
    Hive.registerAdapter<PushNotificationModel>(PushNotificationModelAdapter());
  }

  last documentDirectory = await getApplicationDocumentsDirectory();
  Hive.init(documentDirectory.path);
  if (Hive.isBoxOpen(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST)) {
    await Hive.field<PushNotificationModel>(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST).shut();
  }
  if (Hive.isBoxOpen(HiveBoxConstant.NOTIFICATION_LIST)) {
    await Hive.field<PushNotificationModel>(HiveBoxConstant.NOTIFICATION_LIST).shut();
  }
  await Hive.openBox<PushNotificationModel>(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST);
  await Hive.openBox<PushNotificationModel>(HiveBoxConstant.NOTIFICATION_LIST);

  await FCMManager.saveNotificationList(message, true);

  if (Hive.isBoxOpen(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST)) {
    await Hive.field<PushNotificationModel>(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST).shut();
  }
  if (Hive.isBoxOpen(HiveBoxConstant.NOTIFICATION_LIST)) {
    await Hive.field<PushNotificationModel>(HiveBoxConstant.NOTIFICATION_LIST).shut();
  }
}

void principal() async {
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

...

  await Firebase.initializeApp(choices: DefaultFirebaseOptions.currentPlatform);


  runApp(
  ...
  );
}

last GlobalKey<NavigatorState> navigatorStateGlobalKey = GlobalKey<NavigatorState>();

class AppMain extends ConsumerStatefulWidget {
  const AppMain({Key? key}) : tremendous(key: key);

  @override
  ConsumerState<AppMain> createState() => _AppMainState();
}

class _AppMainState extends ConsumerState<AppMain> with WidgetsBindingObserver {


  @override
  void initState() {
    tremendous.initState();

    FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  }

  @override
  Widget construct(BuildContext context) {
    return MaterialApp(
      ...
    );
  }

}