无法在 iOS 上接收来自 Firebase 3.2.0 主题的推送通知 [英] Cannot receive push notification on iOS from Firebase 3.2.0 topics
问题描述
我在 https://firebase.google 上遵循了 google 的教程.com/docs/notifications/ios/console-topics#receive_and_handle_topic_messages 在我的 iOS 应用上订阅 Firebase 主题.
I followed the tutorial by google on https://firebase.google.com/docs/notifications/ios/console-topics#receive_and_handle_topic_messages to subscribe to a Firebase topic on my iOS app.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
FIRMessaging.messaging().subscribeToTopic("/topics/Notifications")
let homeViewController = UINavigationController(rootViewController: HomeViewController())
UINavigationBar.appearance().translucent = false
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.rootViewController = homeViewController
window?.makeKeyAndVisible()
return true
}
但是,当我从 Firebase 控制台发送主题推送通知时.我无法收到任何推送通知.但是当我从控制台向用户段发送推送通知时,推送工作正常.当我检查 Xcode 控制台时,我看到了这个 FIRMessaging 错误.
However, when I send a topic push notification out from the Firebase console. I could not receive any push notifications. But when I send out push notification to user segment from the console, the push is working perfectly. When I check the Xcode console, I am seeing this FIRMessaging error.
2016-05-31 11:11:47.893: <FIRMessaging/WARNING> Cannot subscribe to topic: /topics/Notifications with token: (null)
我试图搜索这个错误,但没有找到任何东西.我不确定这是否是导致我的应用程序无法从主题接收任何推送的问题.
I've tried to search for this error but have no luck finding anything. I am not sure if this is the problem that is causing my app to not receive any push from topics.
有人遇到过这个问题并且知道如何解决吗?
Does anyone have this issue and know how to solve it?
推荐答案
看起来您可能过早地调用 subscribeToTopic
.
Looks like maybe you're calling subscribeToTopic
too early.
首先,在设置任何 Firebase 调用之前,请确保调用
First, before you set up any Firebase call, make sure you call
FIRApp.configure()
这将确保正确设置和初始化所有 Firebase 服务.
That will ensure that all Firebase services are properly set up and initialized.
接下来,您需要稍等片刻才能订阅主题.您的客户端需要首先向 APNs 和 FCM 注册您的应用程序,以确保它可以接收通知.这涉及网络调用,这意味着您无法在应用程序首次启动时订阅主题.
Next, you're going to need to wait just a bit to subscribe to topics. Your client needs to first register your app with both APNs and FCM to ensure that it can receive notifications. That involves a network call, which means you can't subscribe to topics when your app first launches.
相反,我建议将该代码放入您的 application:didRegisterUserNotificationSettings
处理程序中.像这样:
Instead, I'd recommend putting that code into your application:didRegisterUserNotificationSettings
handler instead. Something like this:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(@"Hooray! I'm registered!");
[[FIRMessaging messaging] subscribeToTopic:@"/topics/cool_users"];
}
还有 Swift 版本...
And the Swift version...
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
print("Hooray! I'm registered!")
FIRMessaging.messaging().subscribeToTopic("/topics/swift_fans")
}
这篇关于无法在 iOS 上接收来自 Firebase 3.2.0 主题的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!