- Both ios and AOS receive background notification normally.
- In AOS, received background notification data can be stored in Hive through backgroundHandler.
- But in ios BackgroundHander doesn t work.
- If BackgroundHandler is not supported on iOS, is there another way?
- Is it not possible to save background notification data to Hive in iOS?
@pragma( vm:entry-point )
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
if (!Hive.isAdapterRegistered(1)) {
Hive.registerAdapter<PushNotificationModel>(PushNotificationModelAdapter());
}
final documentDirectory = await getApplicationDocumentsDirectory();
Hive.init(documentDirectory.path);
if (Hive.isBoxOpen(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST)) {
await Hive.box<PushNotificationModel>(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST).close();
}
if (Hive.isBoxOpen(HiveBoxConstant.NOTIFICATION_LIST)) {
await Hive.box<PushNotificationModel>(HiveBoxConstant.NOTIFICATION_LIST).close();
}
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.box<PushNotificationModel>(HiveBoxConstant.BACKGROUND_NOTIFICATION_LIST).close();
}
if (Hive.isBoxOpen(HiveBoxConstant.NOTIFICATION_LIST)) {
await Hive.box<PushNotificationModel>(HiveBoxConstant.NOTIFICATION_LIST).close();
}
}
void main() async {
WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
...
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
runApp(
...
);
}
final GlobalKey<NavigatorState> navigatorStateGlobalKey = GlobalKey<NavigatorState>();
class AppMain extends ConsumerStatefulWidget {
const AppMain({Key? key}) : super(key: key);
@override
ConsumerState<AppMain> createState() => _AppMainState();
}
class _AppMainState extends ConsumerState<AppMain> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
...
);
}
}