didRegisterForRemoteNotificationsWithDeviceToken is not getting called Swift 5, Xcode 10.2

允我心安 提交于 2020-03-04 06:55:23

问题


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.

回答1:


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




回答2:


I just solved this problem out!! I have no idea why this works, but it works, here is my action:

  1. Add some print in didRegisterForRemoteNotificationsWithDeviceToken method and keep devices connected.

  2. Enter https://developer.apple.com/ Choose Account -> Certificates, Identifiers & Profiles -> Keys

  3. Select the key of your project, click "edit", and close (don't save)
  4. See the log from Xcode, I see many print from didRegisterForRemoteNotificationsWithDeviceToken



回答3:


I just update my icloud settings of my iPhone and login to my developer account and didRegisterForRemoteNotificationsWithDeviceToken being called. That's work for me.



来源:https://stackoverflow.com/questions/55823643/didregisterforremotenotificationswithdevicetoken-is-not-getting-called-swift-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!