有没有办法在flutter中收到FCM通知时触发功能



当应用程序收到通知时,我想在iPhone上的应用程序图标上显示一个徽章,这样用户在看到应用程序图标时就可以注意到他们收到了通知。

以下代码无法像onMessageOpenedApp那样正常工作,当用户点击通知时将触发checkForInitialMessage((,但不会在通知到达时触发。当iPhone收到通知时,我如何触发setBadge((?

我使用的是firebase_messaging 10.0.2和flatter_app_badger。

// App is on background
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
setBadge();
});
// App is terminated
checkForInitialMessage() async {
await Firebase.initializeApp();
RemoteMessage initialMessage = await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
setBadge();
}
}

您可能必须使用FirebaseMessaging.onBackgroundMessage来注册后台消息处理程序,如下所示:

FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler);
static Future<dynamic> _backgroundMessageHandler(RemoteMessage message) async {
setBadge();
}

以下是官方文件:https://firebase.flutter.dev/docs/messaging/usage/#background-消息

最新更新