简体   繁体   English

无法从Firebase 3.2.0主题接收iOS上的推送通知

[英]Cannot receive push notification on iOS from Firebase 3.2.0 topics

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. 我在https://firebase.google.com/docs/notifications/ios/console-topics#receive_and_handle_topic_messages上按照谷歌的教程来订阅我的iOS应用上的Firebase主题。

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
}

However, when I send a topic push notification out from the Firebase console. 但是,当我从Firebase控制台发送主题推送通知时。 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. 当我检查Xcode控制台时,我看到了这个FIRMessaging错误。

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? 有没有人有这个问题,知道如何解决它?

Looks like maybe you're calling subscribeToTopic too early. 看起来好像你太早称之为subscribeToTopic

First, before you set up any Firebase call, make sure you call 首先,在设置任何Firebase呼叫之前,请务必致电

FIRApp.configure()

That will ensure that all Firebase services are properly set up and initialized. 这将确保正确设置和初始化所有Firebase服务。

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. 您的客户需要首先使用APN和FCM注册您的应用程序,以确保它可以接收通知。 That involves a network call, which means you can't subscribe to topics when your app first launches. 这涉及网络呼叫,这意味着您无法在应用首次启动时订阅主题。

Instead, I'd recommend putting that code into your application:didRegisterUserNotificationSettings handler instead. 相反,我建议将该代码放入您的application:didRegisterUserNotificationSettings handler。 Something like this: 像这样的东西:

- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
  NSLog(@"Hooray! I'm registered!");
  [[FIRMessaging messaging] subscribeToTopic:@"/topics/cool_users"];
}

Edit: And the Swift version... 编辑:和Swift版本......

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
  print("Hooray! I'm registered!")
  FIRMessaging.messaging().subscribeToTopic("/topics/swift_fans")
}

The accepted solution did not work for me. 接受的解决方案对我不起作用。 The token is not always available when application:didRegisterUserNotificationSettings: is called. application:didRegisterUserNotificationSettings:被调用时,令牌并不总是可用。 For example if application is freshly installed and starts for the first time FIRInstanceID.instanceID().token() returns nil. 例如,如果应用程序是新安装的并且首次启动FIRInstanceID.instanceID().token()将返回nil。

You need to make sure application calls subscribeToTopic: after the token is available. 您需要确保应用程序调用subscribeToTopic:令牌可用 I ended up with creating a helper object that enqueues subscribeToTopic: , unsubscribeFrom: calls and executes them in FIFO order after the token arrives. 我最终创建了一个帮助对象,该对象将subscribeToTopic:unsubscribeFrom:调用,并在令牌到达后以FIFO顺序执行它们。

class FIRMessagingHelper {

    private let queue: OperationQueue

    init() {
        queue = OperationQueue()
        queue.maxConcurrentOperationCount = 1
        queue.addOperation(TokenReadyOperation())
    }

    func subscribeTo(topic: String) {
        queue.addOperation { 
            OperationQueue.main.addOperation({ 
                FIRMessaging.messaging().subscribeToTopic(topic)
            })
        }
    }

    func unsubscribeFrom(topic: String) {
        queue.addOperation {
            OperationQueue.main.addOperation({
                FIRMessaging.messaging().unsubscribeFromTopic(topic)
            })
        }
    }
}

TokenReadyOperation waits until the token appears. TokenReadyOperation等待,直到出现令牌。 AsynchronousOperation is used as the base class to minimize boilerplate. AsynchronousOperation用作最小化样板的基类。

class TokenReadyOperation : AsynchronousOperation {

    override init() {
        super.init()
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(TokenReadyOperation.tokenRefreshed(notification:)),
                                               name: .firInstanceIDTokenRefresh,
                                               object: nil)
    }

    override func didStart() {
        finishIfTokenAvailable()
    }

    private func finishIfTokenAvailable() {
        guard FIRInstanceID.instanceID().token() != nil else { return }
        markFinished()
    }

    /// Posted every time token changes
    @objc private func tokenRefreshed(notification: Notification) {
        finishIfTokenAvailable()
    }
}

Few things to keep in mind: 要记住几件事:

  • App must call FIRApp.configure() or FIRApp.configureWithOptions(_:) prior making any Firebase calls (as Todd Kerpelman mentioned) 应用程序必须在进行任何Firebase调用之前调用FIRApp.configure()FIRApp.configureWithOptions(_:) )(如Todd Kerpelman所述)
  • subscribeToTopic: , unsubscribeFrom: are not thread safe and must be executed on main thread subscribeToTopic:unsubscribeFrom:不是线程安全的,必须在主线程上执行
  • Topic names has to be in "/topics/*" format (as henmer mentioned) 主题名称必须采用“/ topics / *”格式(如henmer所述)
  • Make sure to use different configuration plist for debug and App Store release of your app. 确保使用不同的配置plist进行应用程序的调试和App Store发布。 See FIRApp.configureWithOptions(_:) documentation. 请参阅FIRApp.configureWithOptions(_:)文档。
  • Date & Time should be current, otherwise the token may not be delivered. 日期和时间应该是最新的,否则可能无法交付令牌。
  • Make sure to use the newest framework version . 确保使用最新的框架版本 I had issues with notification delivery with the SDK released around January 2017. 2017年1月左右发布的SDK发布了通知问题。

My problem was not solved by calling subscribeToTopic after 之后调用subscribeToTopic没有解决我的问题

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {

instead it worked by calling subscribeToTopic after 相反,它通过调用subscribeToTopic后工作

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

this function get called when you get your push token from APNS not firebase. 当您从APNS获取推送令牌而不是firebase时,将调用此函数。

Xcode 8.3.2 Xcode 8.3.2

Swift 3.0 Swift 3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM