iosswiftpush-notification

didRegisterForRemoteNotificationsWithDeviceToken is not getting called Swift 5, Xcode 10.2


didRegisterForRemoteNotificationsWithDeviceToken and didFailToRegisterForRemoteNotificationsWithError is not getting called on real device.

This what i have tried so far:

In didFinishLaunchingWithOptions

    FirebaseApp.configure()

    if #available(iOS 10.0, *) {
        let center = UNUserNotificationCenter.current()
        center.delegate = self
        center.requestAuthorization(options: [.badge, .alert, .sound]) {
            (granted, error) in
            if granted {
                DispatchQueue.main.async {
                    application.registerForRemoteNotifications()
                    //UIApplication.shared.registerForRemoteNotifications()
                }
            } else {
                //print("APNS Registration failed")
                //print("Error: \(String(describing: error?.localizedDescription))")
            }
        }
    } else {
        let type: UIUserNotificationType = [UIUserNotificationType.badge, UIUserNotificationType.alert, UIUserNotificationType.sound]
        let setting = UIUserNotificationSettings(types: type, categories: nil)
        application.registerUserNotificationSettings(setting)
        application.registerForRemoteNotifications()
        //UIApplication.shared.registerForRemoteNotifications()
    }

Then the register and fail method:

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

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {

        print(error.localizedDescription)
        print("Not registered notification")
}

Note:

  • I have tried it on real device not neither of the methods are getting called.
  • I have tried on simulator if the code is working or not, didFailToRegisterForRemoteNotificationsWithError is getting called with error.(Which means code is fine)
  • I have double checked the certificates and regenerated the provisioning file after turning on the push notifications in capabilities.
  • I have also added background modes -> remote notifications on.
  • I have tried with legacy build also no luck.
  • I have tried reinstalling apps many times not working.
  • FirebaseAppDelegateProxyEnabled is set to NO in plist still no luck.
  • Also updated the pods still no luck.

Solution

  • As we discussed on Chat. Firebase use method swizzling so you will get token in func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) method instead of didRegisterForRemoteNotificationsWithDeviceToken

    You have to make sure following things

    1) Correct Certificate for Prod. and Dev.

    2) Must upload certi. to firebase console

    3) Must implement UNUserNotificationCenter's Delegate method

    4) make sure you have implemented func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

    5) make sure you have a line completionHandler() at bottom of the function

    Hope it is helpful