- Each ios and AOS obtain background notification usually.
- In AOS, obtained background notification knowledge could be saved in Hive by means of backgroundHandler.
- However in ios BackgroundHander would not work.
- If BackgroundHandler isn’t supported on iOS, is there one other approach?
- Is it not doable to save lots of 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 essential() 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(
...
);
}
}