xcode Firebase FCM 实例 ID 为零(在真实设备上运行快速入门应用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38030081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:57:58  来源:igfitidea点击:

Firebase FCM Instance ID is nil (running quickstart app on real device)

objective-ciphonexcodefirebase-cloud-messaging

提问by IanS

Running the FCM demo app on my iPhone doesn't work - The Firebase ID is nil.

在我的 iPhone 上运行 FCM 演示应用程序不起作用 - Firebase ID 为nil

Steps to reproduce:

重现步骤:

  1. pod try Firebase, and run 10: quickstart-ios/messaging/FCM.xcodeproj
  2. Copy my GoogleService-Info.plistfile into the project
  3. Run the app on an iPhone 5s (iOS 9.3.2)
  4. Accept the dialog requesting permission to send notifications
  5. Click the buttons
  6. The Instance Id token is nil
  1. pod try Firebase,并运行 10: quickstart-ios/messaging/FCM.xcodeproj
  2. 将我的GoogleService-Info.plist文件复制到项目中
  3. 在 iPhone 5s (iOS 9.3.2) 上运行该应用程序
  4. 接受请求发送通知权限的对话框
  5. 单击按钮
  6. 实例 ID 令牌为零

My Mac has the appropriate certificates to develop and deploy apps:

我的 Mac 拥有相应的证书来开发和部署应用程序:

  1. I have both iOS distribution and iOS development provisioning profiles, chosen using the 'automatic' settings.
  2. I have both iOS distribution and iOS development certificates installed, code-signed using the 'automatic: iPhone developer' option
  3. I have the 'Production' SSL certificate installed: "Apple Push Notification service SSL (Sandbox & Production)"
  1. 我有 iOS 发行版和 iOS 开发配置文件,使用“自动”设置选择。
  2. 我安装了 iOS 发行版和 iOS 开发证书,使用“自动:iPhone 开发人员”选项进行了代码签名
  3. 我安装了“生产”SSL 证书:“Apple 推送通知服务 SSL(沙盒和生产)”

I see lots of noise in the logs, but the most significant error is this one:

我看到日志中有很多噪音,但最重要的错误是这个:

Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"

Here is the full console output:

这是完整的控制台输出:

2016-06-25 16:15:08.339 FCM[7398:4128979] Configuring the default app.
2016-06-25 16:15:08.383 FCM[7398:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started
2016-06-25 16:15:08.396: <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-06-25 16:15:08.397: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
2016-06-25 16:15:08.403: <FIRMessaging/INFO> FIRMessaging library version 1.1.0
2016-06-25 16:15:08.413: <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-06-25 16:15:08.443 FCM[7398:] <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-06-25 16:15:08.508 FCM[7398:] <FIRAnalytics/INFO> Firebase Analytics enabled
2016-06-25 16:15:08.538: <FIRInstanceID/WARNING> APNS Environment in profile: development
2016-06-25 16:15:08.848 FCM[7398:4128979] Connected to FCM.
2016-06-25 16:15:08.893: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:15:22.311: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:15:36.774 FCM[7398:4128979] Unable to connect to FCM. Error Domain=com.google.fcm Code=2001 "(null)"
2016-06-25 16:15:40.552 FCM[7398:4128979] Subscribed to news topic
2016-06-25 16:15:43.100 FCM[7398:4128979] InstanceID token: (null)
2016-06-25 16:15:49.036: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:16:31.022: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:17:32.976: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:17:32.976: <FIRInstanceID/WARNING> Failed to retrieve the default GCM token after 5 retries
2016-06-25 16:19:10.662 FCM[7398:4128979] InstanceID token: (null)
2016-06-25 16:19:10.980: <FIRInstanceID/WARNING> Failed to fetch default token Error Domain=com.firebase.iid Code=0 "(null)"
2016-06-25 16:19:10.981: <FIRInstanceID/WARNING> Failed to retrieve the default GCM token after 5 retries

采纳答案by IanS

As well as installing the APNs SSL certificates on your dev machine, you also need to upload them to the Firebase Console!

除了在您的开发机器上安装 APNs SSL 证书外,您还需要将它们上传到 Firebase 控制台!

https://console.firebase.google.com/project/your-project-name-here/settings/cloudmessaging

https://console.firebase.google.com/project/ your-project-name-here/settings/cloudmessaging

回答by Lucas Chwe

Try updating Firebase/Core to v3.4.4, it fixed unexpected errors for me. Otherwise the typical fixes are:

尝试将 Firebase/Core 更新到 v3.4.4,它为我修复了意外错误。否则,典型的修复是:

  • Enabling Push Notificationsunder the app's Capabilities
  • Avoid calling unregisterForRemoteNotifications. After this call, you can't register the device anymore to push notifications.
  • Setting FirebaseAppDelegateProxyEnabledto NOin your info.plist file. I think there are bugs with their method swizzling.
  • Calling [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeUnknown]in the method didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  • Push Notifications在应用程序下启用Capabilities
  • 避免调用unregisterForRemoteNotifications。此调用后,您无法再注册设备以推送通知。
  • 在 info.plist 文件中设置FirebaseAppDelegateProxyEnabledNO。我认为他们的方法存在错误。
  • [[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeUnknown]在方法中调用didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken

回答by IanS

I just ran the same code on the simulator (iPhone 5, iOS 9.3), and I get a token without any trouble.

我只是在模拟器(iPhone 5、iOS 9.3)上运行了相同的代码,并且毫无问题地获得了令牌。

I do see lots of the same warnings, though...

不过,我确实看到了很多相同的警告......

2016-06-25 16:38:31.353 FCM[2546:42455] Configuring the default app.
2016-06-25 16:38:32.561 FCM[2546:] <FIRAnalytics/INFO> Firebase Analytics v.3200000 started
2016-06-25 16:38:32.833 FCM[2546:] <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-06-25 16:38:32.901: <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-06-25 16:38:32.928: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(null)"
2016-06-25 16:38:32.971: <FIRMessaging/INFO> FIRMessaging library version 1.1.0
2016-06-25 16:38:33.189: <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-06-25 16:38:51.094: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=NSCocoaErrorDomain Code=3010 "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION" UserInfo={NSLocalizedDescription=REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}
2016-06-25 16:38:51.096: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=NSCocoaErrorDomain Code=3010 "REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION" UserInfo={NSLocalizedDescription=REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}
2016-06-25 16:38:51.140 FCM[2546:42883] CFNetwork SSLHandshake failed (-9806)
2016-06-25 16:38:51.141 FCM[2546:42883] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9806)
2016-06-25 16:38:51.460 FCM[2546:42455] Connected to FCM.
2016-06-25 16:38:56.709 FCM[2546:42455] InstanceID token: fgbPv5tnsPk:APA91bHUWOmuLmd4UkLXcp3Lysp92fH6bOb7tfmbjHehFOt_NPnc6tnMk-7YT51VASkk-dyHZAl01QRcedwqGUhHMiIXeV_xknBXppncu0WyjKLYhaVQ08bYKNZ5M1CuSjtk9l0HPyJX
2016-06-25 16:38:56.711 FCM[2546:42455] Unable to connect to FCM. Error Domain=com.google.fcm Code=2001 "(null)"
2016-06-25 16:38:57.505 FCM[2546:] <FIRAnalytics/INFO> Firebase Analytics enabled
2016-06-25 16:40:03.845 FCM[2546:42455] Unable to connect to FCM. Error Domain=com.google.fcm Code=2001 "(null)"
2016-06-25 16:40:06.534 FCM[2546:42455] InstanceID token: fgbPv5tns9......

回答by IanS

The issue was due to my use of the 'Production' SSL certificate, instead of the 'Development' one. Installing that seemed to fix things.

问题是由于我使用了“生产”SSL 证书,而不是“开发”证书。安装它似乎可以解决问题。

I had wrongly assumed that both development and production versions would be handled by the "Apple Push Notification service SSL (Sandbox & Production)" certificate. That may be the case for native APNs, but it doesn't work for Firebase.

我错误地认为开发和生产版本都将由“Apple Push Notification service SSL (Sandbox & Production)”证书处理。原生 APN 可能就是这种情况,但它不适用于 Firebase。

回答by IanS

If you have ever happened to run this command:

如果你曾经碰巧运行过这个命令:

[[UIApplication sharedApplication] unregisterForRemoteNotifications],

your app will not be able to register for remote notifications.

您的应用将无法注册远程通知。

registerusernotificationsettings not work after using unregisterForRemoteNotifications

使用 unregisterForRemoteNotifications 后 registerusernotificationsettings 不起作用

You need to uninstall the app to get it working again...

您需要卸载该应用程序才能使其再次运行...

回答by Ng?c Nguy?n

For me, If happened when i do not use FIRAuth before get values from Firebase.

对我来说,如果在从 Firebase 获取值之前我不使用 FIRAuth 时发生了这种情况。

FIRAuth.auth()?.signIn(withEmail: "[email protected]", password: "123456", completion: { (user, error) in
if(error != nil){
    print(error)
    return
}


let ref = FIRDatabase.database().reference(fromURL: "https://firdemo-84b6b.firebaseio.com/").child("users")

ref.observe(.value, with: { (snapshot) in
    print(snapshot.key)
    print(snapshot.value)
    }, withCancel: { (error) in
        if(error != nil){
            print(error)
            return
        }
    })

})

Please try it. And let me know if it does not work.

请尝试一下。如果它不起作用,请告诉我。

回答by Prashant