[英]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.