【问题标题】:Firebase Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"Firebase 无法获取 APNS 令牌错误域 = com.firebase.iid 代码 = 1001 “(空)”
【发布时间】:2016-08-23 00:44:23
【问题描述】:
2016-08-22 18:34:50.108: <FIRInstanceID/WARNING> FIRInstanceID AppDelegate proxy enabled, will swizzle app delegate remote notification handlers. To disable add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2016-08-22 18:34:50.106 YAKKO[4269:] <FIRAnalytics/INFO> To enable debug logging set the following application argument: -FIRAnalyticsDebugEnabled (see ...)
2016-08-22 18:34:50.114: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
2016-08-22 18:34:50.120: <FIRMessaging/INFO> FIRMessaging library version 1.1.1
2016-08-22 18:34:50.125: <FIRMessaging/WARNING> FIRMessaging AppDelegate proxy enabled, will swizzle app delegate remote notification receiver handlers. Add "FirebaseAppDelegateProxyEnabled" to your Info.plist and set it to NO
2016-08-22 18:34:50.144 YAKKO[4269:] <FIRAnalytics/INFO> Successfully created Firebase Analytics App Delegate Proxy automatically. To disable the proxy, set the flag FirebaseAppDelegateProxyEnabled to NO in the Info.plist
2016-08-22 18:34:50.188 YAKKO[4269:] <FIRAnalytics/INFO> Firebase Analytics enabled

有人问过这个问题before。但是我仍然不知道如何解决它。通知不起作用,我唯一的线索是:Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"

我已经仔细检查了我是否将正确的 .p12 文件上传到了 firebase。我已进入目标->-> 功能-> 后台模式->remote-notificationsON 我已经仔细检查了我的 bundleID 是否与 GoogleService-Info.plist 匹配

这是我的 AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        FIRApp.configure()
        ....
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("DEVICE TOKEN = \(deviceToken)")
    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
                 fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
    // If you are receiving a notification message while your app is in the background,
    // this callback will not be fired till the user taps on the notification launching the application.
    // TODO: Handle data of notification
    FIRMessaging.messaging().appDidReceiveMessage(userInfo)

    // Print message ID.
    print("Message ID: \(userInfo["gcm.message_id"]!)")

    // Print full message.
    print("%@", userInfo)
}
func tokenRefreshNotification(notification: NSNotification) {
    // NOTE: It can be nil here
    let refreshedToken = FIRInstanceID.instanceID().token()
    print("InstanceID token: \(refreshedToken)")

    connectToFcm()
}

func connectToFcm() {
    FIRMessaging.messaging().connectWithCompletion { (error) in
        if (error != nil) {
            print("Unable to connect with FCM. \(error)")
        } else {
            print("Connected to FCM.")
        }
    }
}

我的猜测是,如果没有最后两种方法,它应该可以工作,但我还是把它们放在那里(根据 print 语句,它看起来不像被调用)。

我调用 registerForRemoteNotifications:

static func registerForNoties(){

    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
    let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(pushNotificationSettings)
    UIApplication.sharedApplication().registerForRemoteNotifications()
}

我可以看到控制台上打印的 deviceToken。但我仍然遇到这个 FireBase 问题。我可以检查的其他内容有什么想法吗?

【问题讨论】:

  • 这个问题你解决了吗?
  • 不,我最终放弃了 Firebase for iOS 并使用了 PyAPNS(我的后端是用 django/Python 编写的)。

标签: ios firebase firebase-cloud-messaging


【解决方案1】:

我确实尝试基于 Firebase/Quickstart-iOS 示例为 iOS (Swift) 构建简单的应用程序,但我遇到了同样的问题:Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"

当应用程序在后台时,我无法收到通知。 所以,我在这里找到了适合我的解决方案: https://github.com/firebase/quickstart-ios/issues/103

基本上,我确实添加了这个方法:

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
            print("Handle push from background or closed" );
            print("%@", response.notification.request.content.userInfo);
        }

...和这行代码:

application.registerForRemoteNotifications()

显然,用 Objective-C 编写的示例可以正常工作,但 Swift 中的示例缺少一些代码,并且无法按预期工作...

【讨论】:

    【解决方案2】:

    尝试将 Firebase/Core 更新到 v3.4.4,它为我修复了意外错误。否则尽量避免调用unregisterForRemoteNotifications。在此呼叫之后,您无法再注册设备以推送通知。另外,尝试在 info.plist 文件中将FirebaseAppDelegateProxyEnabled 设置为NO。我认为他们的方法混乱存在错误。

    【讨论】:

      【解决方案3】:

      调用这个方法你会得到设备令牌。我可以看到控制台上打印的deviceToken

      dispatch_async(dispatch_get_main_queue(),
      {
           global.appdel.tokenRefreshNotification()
      })
      

      【讨论】:

      • 你应该在第一个屏幕出现的地方调用这个方法。在 appdelegate 之后。
      • 在 viewdidload() 中添加这个方法
      猜你喜欢
      • 2016-12-18
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      相关资源
      最近更新 更多