且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Flutter在应用程序图标上显示通知徽章?

更新时间:2022-12-22 11:54:15

最新答案,但是,关于您的问题,我认为您希望像下面的图片一样向应用程序图标添加数量.

Late answer but, on your question i think you want to add count to the app icon just like the image below.

因此,对于此问题,您可以使用 flutter_app_badger .使用此软件包,您可以将计数添加到您的应用程序图标中.

So for this issue you can use flutter_app_badger. Using this package you can add count to your app icon.

要在 FCM 中使用 flutter_app_badger ,您可以使用此

To use flutter_app_badger with FCM you can use this

 _firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    //Here you can add the count when you get a notification you can increase the number by one
    FlutterAppBadger.updateBadgeCount(1);
  },
  onBackgroundMessage: myBackgroundMessageHandler,
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
    //Here you can remove the badge you created when it's launched
    FlutterAppBadger.removeBadge();
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);

然后,您还可以将其添加到后台通知处理程序中

Then you can also add it to the background notification handler

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
   //Here you can add 
   FlutterAppBadger.updateBadgeCount(1);

   ...
   // Or do other work.
}

请记住,只有在有效负载上没有通知时,才会在ios上触发后台处理程序.您可以在 answer 中阅读有关此问题的更多信息.

Remember, On ios background handler is triggered when only there is no notification on the payload. You can read my more about this issue on my answer.