Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. 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?

like image 970
howly Avatar asked May 31 '16 15:05

howly


People also ask

Can I use Firebase for push notification to iOS?

One way to implement push notifications for iOS apps is through Firebase Notifications.

How do I enable push notifications on iOS?

Go to Settings and tap Notifications. Select an app under Notification Style. Under Alerts, choose the alert style that you want. If you turn on Allow Notifications, choose when you want the notifications delivered — immediately or in the scheduled notification summary.

How do I get Firebase to push notifications?

Go to Firebase console — →Project Settings — →Cloud Messaging. To send the message select Body — →Raw — →JSON(application/json). You can send Notification Payload , Data Payload and even both using POSTMAN service.


1 Answers

Looks like maybe you're calling subscribeToTopic too early.

First, before you set up any Firebase call, make sure you call

FIRApp.configure()

That will ensure that all Firebase services are properly set up and initialized.

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.

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"];
}

Edit: And the Swift version...

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
  print("Hooray! I'm registered!")
  FIRMessaging.messaging().subscribeToTopic("/topics/swift_fans")
}
like image 128
Todd Kerpelman Avatar answered Sep 21 '22 08:09

Todd Kerpelman