代码之家  ›  专栏  ›  技术社区  ›  Kleyson Rios

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

  •  0
  • Kleyson Rios  · 技术社区  · 6 年前

    我正在使用FCM在flutter应用程序中推送通知。

    我尝试了很多东西和代码,当新的通知到达并且应用程序关闭或处于后台时,在应用程序图标顶部显示一个徽章(红点)。

    我该怎么做才能让徽章应用程序图标动起来?

    1 回复  |  直到 6 年前
        1
  •  26
  •   Abel Tilahun    5 年前

    回答得晚了,但关于你的问题,我认为你想像下图一样在应用程序图标上添加计数。

    Notification badge on ios

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

    使用 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后台处理程序。 你可以在我的 answer .

        2
  •  1
  •   C.K.    4 年前

    试试这个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>"
    }
    

    enter image description here enter image description here