didRegisterForRemoteNotificationsWithDeviceToken没有被称为Swift 5,Xcode 10.2 [英] didRegisterForRemoteNotificationsWithDeviceToken is not getting called Swift 5, Xcode 10.2

查看:398
本文介绍了didRegisterForRemoteNotificationsWithDeviceToken没有被称为Swift 5,Xcode 10.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

didRegisterForRemoteNotificationsWithDeviceTokendidFailToRegisterForRemoteNotificationsWithError.

这是我到目前为止尝试过的:

This what i have tried so far:

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()
    }

然后注册并失败方法:

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")
}

注意:

  • 我已经在真实设备上尝试过,但没有一个方法被调用.
  • 我已经在模拟器上尝试了代码是否正常工作,是否调用了didFailToRegisterForRemoteNotificationsWithError 错误.(这意味着代码很好)
  • 我在打开了推送通知后仔细检查了证书并重新生成了配置文件 功能.
  • 我还添加了后台模式->启用了远程通知.
  • 我尝试过旧版构建,也没有运气.
  • 我已尝试多次重新安装应用程序,但无法正常运行.
  • FirebaseAppDelegateProxyEnabled在plist中设置为NO还是没有运气.
  • 还更新了豆荚,仍然没有运气.
  • 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.

推荐答案

正如我们在聊天"中讨论的那样. Firebase使用方法混乱,因此您将在func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String)方法而不是didRegisterForRemoteNotificationsWithDeviceToken

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)正确的产品证书.和开发.

1) Correct Certificate for Prod. and Dev.

2)必须上传证书.到Firebase控制台

2) Must upload certi. to firebase console

3)必须实现UNUserNotificationCenter的Delegate方法

3) Must implement UNUserNotificationCenter's Delegate method

4)确保已实现func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)

5)确保函数底部有一行completionHandler()

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

希望对您有帮助

这篇关于didRegisterForRemoteNotificationsWithDeviceToken没有被称为Swift 5,Xcode 10.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆