ios 如何在Flutter中的应用图标上显示通知计数?

az31mfrm  于 12个月前  发布在  iOS
关注(0)|答案(2)|浏览(242)

在android设备中,我猜它的默认工作方式是预期的。但在iOS设备中,它不是这样工作的。我搜索了很多,我发现一个包名为:flutter_app_badger
我使用Firebase Cloud Messaging(FCM)进行通知。正如预期的那样,工作得很好。我增加徽章计数与通知。App内部运行如预期。但当它在后台或终止其不增加计数在徽章。我该怎么解决呢?这是我的代码显示通知和增量徽章。

Future<void> onBackgroundMessageHandler(RemoteMessage message) async {
 
  LocalNotificationService.display(message);
}

FirebaseMessaging.onBackgroundMessage(onBackgroundMessageHandler);  //Inside

 FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage? message) async {
      if (message != null) {
        LocalNotificationService.display(message);
      }
    });

    FirebaseMessaging.onMessage.listen((message) async {
      inspect(message);
      if (message.notification != null) {
        log("tıkladı");
      }
      LocalNotificationService.display(message);
    });

    //App is not terminatd but its in background
    FirebaseMessaging.onMessageOpenedApp.listen((message) {
      LocalNotificationService.display(message);
    });

Display方法显示通知,并增加徽章,如:

if (await FlutterAppBadger.isAppBadgeSupported()) {
        final countCache = await 
        UserSecureStorage.getField("notificationBadgeCount"); 
        final int count = int.parse(countCache ?? "0") + 1;
        await UserSecureStorage.setField("notificationBadgeCount", count.toString());
        await FlutterAppBadger.updateBadgeCount(count);
      }

但它并没有像我说的那样在背景中增加或终止。我该如何解决这个问题?感谢您的回复。

7jmck4yq

7jmck4yq1#

我以前遇到过你面临的问题。它与FCM或flutter_app_badger无关,它与发送给FCM的通知体有关。您应该像下面的REST示例一样更新/修改通知正文:

{
   "message":{
      "token":"XXXXXXXXX",
      "data":{},
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!"
      },
      "apns":{
        "payload": {
            "aps": {
                "sound": "default",
                "content-available": 1
            }
        }
      }
   }
}

重要的部分是apns部分:

"apns":{
        "payload": {
            "aps": {
                "sound": "default",
                "content-available": 1
            }
        }
      }
uqzxnwby

uqzxnwby2#

您需要在应用程序托盘中显示通知计数。
找到下面的帖子。有详细的解释:
How to show a notification badge on the app icon using flutter?

相关问题