Flutter - 如何在打开应用程序后删除 iOS 通知徽章

jac*_*per 8 push-notification ios firebase flutter firebase-cloud-messaging

当我从 Firebase Cloud Messaging 发送通知时,通知徽章会显示在应用程序图标上,但没有代码可将其删除。

通知的数量并不重要,所以我不需要任何通知计数器或其他什么,所以我需要做的就是在打开应用程序时清除通知徽章。

如何在不重写整个通知代码的情况下完成此任务(我几乎无法正常工作)?

这是代码:

_fcm.getToken().then((token) {
  print("The token is: " + token);
  setState(() {
    tokenSave = token;
  });
});

_fcm.requestPermission(
  sound: true,
  badge: true,
  alert: true,
  provisional: false,
);

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
  RemoteNotification notification = message.notification;
  AndroidNotification android = message.notification?.android;
  if (notification != null && android != null) {
    flutterLocalNotificationsPlugin.show(
      notification.hashCode,
      notification.title,
      notification.body,
      NotificationDetails(
        android: AndroidNotificationDetails(
          channel.id,
          channel.name,
          channel.description,
          color: Colors.blue,
          playSound: true,
          icon: null,
        ),
      ),
    );
  }
});

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
  print("onMessageOpenedApp event");
  RemoteNotification notification = message.notification;
  AndroidNotification android = message.notification?.android;
  if (notification != null && android != null) {
    showDialog(
        context: context,
        builder: (_) {
          return AlertDialog(
            title: Text(notification.title),
            content: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [Text(notification.body)],
              ),
            ),
          );
        });
  }
});
Run Code Online (Sandbox Code Playgroud)

}

小智 8

将以下函数添加到您的AppDelegate.swift,application()函数下:

override func applicationDidBecomeActive(_ application: UIApplication) {
  application.applicationIconBadgeNumber = 0;
}
Run Code Online (Sandbox Code Playgroud)


小智 2

我以前也遇到过同样的问题。我的解决方法是使用App badger 包

main.dart使用该包,您可以在应用程序的入口点或任何其他入口点执行以下操作:

FlutterAppBadger.removeBadge();
Run Code Online (Sandbox Code Playgroud)

或者

FlutterAppBadger.updateBadgeCount(0);
Run Code Online (Sandbox Code Playgroud)