ios - 为什么我收到通知后无法移除角标(Badge)?

标签 ios swift firebase-cloud-messaging

我正在尝试使用 Firebase 云消息传递实现推送通知,我通过 firebase 控制台发送消息。在 Firebase 控制台中撰写消息时,我将角标(Badge)编号设置为 1,如下图所示

enter image description here

在那之后,我的应用程序在主屏幕上的图标总是有数字“1”的角标(Badge),即使我尝试卸载并重新安装它,数字“1”的角标(Badge)仍然存在。

但它只发生在我的 iPhone 上,如果我在其他手机上安装它,角标(Badge)将不会显示

我在 App delegate 中使用这段代码来触发推送通知

@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
    }





}

如何解决这个问题?

最佳答案

iOS 会始终记住您的应用程序角标(Badge)计数,即使您卸载应用程序并重新安装也是如此。要移除角标(Badge),您必须执行以下任一操作,

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

谢谢。

关于ios - 为什么我收到通知后无法移除角标(Badge)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49354965/

相关文章:

ios - UITextField 不响应点击

firebase - 如何通过HTTP向观众发送Firebase通知

android - 无法使用 firebase admin SDK 在 FCM 中获取数据消息

iOS:为什么 Facebook 注销按钮显示不正确?

ios - 以编程方式设置文本时,不会触发 UITextField 委托(delegate)方法

swift - 如何在 Swift 中创建一个简单的 UIViewController 并将其设置为起始 View

android - 应用程序在前台时收到的推送通知与应用程序在后台时收到的推送通知不同

ios - [UITableView _endCellAnimationsWithContext :],/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UITableView.m:1716

ios - 无法构建 iOS 应用程序, "unable to write module session file Session.modulevalidation No such file or directory"

arrays - 如何让你的tableView在你的tableView顶部显示新的或更新的元素 PT.2