为什么我在收到通知后无法移除徽章?

max*_*ano 6 ios swift firebase-cloud-messaging

我正在尝试使用 Firebase Cloud Messaging 实现推送通知,我通过 Firebase 控制台发送消息。在 Firebase 控制台中撰写消息时,我将徽章编号设置为 1,如下图所示

在此处输入图片说明

在那之后,我在主屏幕中的应用程序图标将始终带有编号为“1”的徽章,即使我尝试卸载并重新安装它,但编号为“1”的徽章仍然存在。

它只发生在我的 iPhone 上,如果我将它安装在另一部手机上,则不会显示徽章

我在 App 委托中使用此代码来触发推送通知

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, MessagingDelegate, UNUserNotificationCenterDelegate {

    var window: UIWindow?
    var fcmTokenUser : String?
    var firsTimeUsingApp = true

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FirebaseApp.configure()


        print(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as String)


        // To get FCM token that will be sent to APNS via Google FCM
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()


        Messaging.messaging().delegate = self
        let token = Messaging.messaging().fcmToken
        fcmTokenUser = token

        checkFirstTimeUsingAppOrNot()
        moveToNextPage()



        // to make status bar in the light mode (in info.plist it also has to be set 'View controller-based status bar appearance' to NO)
        UIApplication.shared.statusBarStyle = .lightContent


        return true
    }



    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String){
         // This callback is fired at each app startup (when the user install the app for the very first time) and whenever a new token is generated due to The app is restored on a new device, The user uninstalls/reinstall the app, The user clears app data.

        // after fcm generated for the very first time,then fcm can also be retrieved in the 'didFinishLaunchingWithOptions' method above (let token = Messaging.messaging().fcmToken)


        fcmTokenUser = fcmToken



        moveToNextPage()



    }




    private func application(application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        Messaging.messaging().apnsToken = deviceToken as Data
    }





}
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

Kar*_*raj 11

即使您卸载应用程序并重新安装,iOS 也会始终记住您的应用程序徽章计数。要移除您的徽章,您必须执行以下任何一项操作,

  1. 使用 向您的应用程序发送另一个推送通知badge = 0
  2. 每当用户使用以下代码打开您的应用程序时,您可以自行删除徽章计数UIApplication.shared.applicationIconBadgeNumber = 0。在Appdelegate's didBecomeActive(:)方法中添加这行代码。

谢谢。

  • 不,如果您设置为 0,它不会将徽章显示为 0。相反,它会删除徽章 :) (3认同)