[英]How to show a notification badge on the app icon using flutter?
我正在使用 FCM 在 flutter 应用程序中推送通知。
当新通知到达并且应用程序关闭或处于后台时,我已经尝试了很多东西和代码来在应用程序图标顶部显示一个徽章(红点)。
flutter怎么弄徽章app图标?
迟到的答案,但是,关于你的问题,我认为你想像下图一样向应用程序图标添加计数。
所以对于这个问题你可以使用flutter_app_badger 。 使用此 package,您可以为您的应用程序图标添加计数。
要将flutter_app_badger与FCM一起使用,您可以使用此
_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");
},
);
然后你也可以将它添加到后台通知处理程序
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
//Here you can add
FlutterAppBadger.updateBadgeCount(1);
...
// Or do other work.
}
请记住,On ios 只有在有效负载上没有通知时才会触发后台处理程序。 您可以在我的回答中阅读有关此问题的更多信息。
试试这个 JSON Body,我得到了徽章计数和声音,仍在试图找出原因,以及如何在阅读后清除它。
{
"notification": {
"body": "This is an FCM notification message!",
"title": "FCM Message",
"sound": "alert.aiff"
},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done"
},
"apns": {
"payload": {
"aps": {
"badge": 9
},
"messageID" : "ABCDEFGHIJ"
}
},
"to": "<the token you want to push to>"
}
我正在使用 FCM 在 flutter 应用程序中推送通知。
当新通知到达并且应用程序关闭或在后台时,我尝试了很多东西和代码来在应用程序图标顶部显示一个徽章(红点)。
我应该怎么做才能在 flutter 中获得徽章应用程序图标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.