如何使用Flutter在应用程序图标上显示通知徽章? [英] How to show a notification badge on the app icon using flutter?

查看:98
本文介绍了如何使用Flutter在应用程序图标上显示通知徽章?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FCM在Flutter应用中推送通知.

I'm using FCM to push notifications in a flutter app.

我尝试了很多事情和代码,以在收到新通知且应用程序关闭或处于后台时在应用程序图标的顶部显示徽章(红点).

I've tried a lot of things and codes to show a badge (red dot) on top of the app icon when a new notification arrives and the app is closed or in background.

我应该怎么做才能在徽章中显示徽章应用程序图标?

What should I do to get a badge app icon in flutter ?

推荐答案

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

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.

这篇关于如何使用Flutter在应用程序图标上显示通知徽章?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆