English 中文(简体)
How or where to store FCM Background notification data in Hive in Flutter iOS?
原标题:
  1. Both ios and AOS receive background notification normally.
  2. In AOS, received background notification data can be stored in Hive through backgroundHandler.
  3. But in ios BackgroundHander doesn t work.
  4. If BackgroundHandler is not supported on iOS, is there another way?
  5. 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(
      ...
    );
  }

}

问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签