Search code examples
iosswiftfirebase-cloud-messaging

why I can't remove the badge after receiving notification?


I am trying to implement push notification using Firebase Cloud Messaging, I send the message through firebase console. when composing a message in Firebase console, I set the badge number to be 1 like the picture below

enter image description here

after that, my app icon in the home screen will always have badge with number "1", even tough I have tried to uninstall and reinstall it, the badge with number "1" is still there.

but it only happens in my iPhone,if I Install it on the other phone, the badge will not show up

I use this code in App delegate to trigger push notification

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





}

how to resolve this issue?


Solution

  • iOS will remember your applications badge count always, even if your uninstall your app and install it again. For removing your badge you have to do any one of following things,

    1. Send another push notification to your app with badge = 0.
    2. Remove badge count by your own whenever user open your app by using below code, UIApplication.shared.applicationIconBadgeNumber = 0 . Add this line of code in Appdelegate's didBecomeActive(:) method.

    Thanks.