26

I'm getting this error from Firebase Messaging API:

[Firebase/Messaging][I-FCM002010] Cannot subscribe to topic: /topics/testTopic with token: (null)

But before:

Messaging.messaging().subscribe(toTopic: "/topics/testTopic")

I'm printing out the token like this:

print("TOKEN: \(InstanceID.instanceID().token() ?? "NO TOKEN")")

The result is:

TOKEN:cXPhGQ_inE4:APA91bEKZF5depHmIm9gDliCFRCRcnJf5LYy5FMg6nhpWvKU3o3HEtr1WTBHUiCZXT4XzhVg2oqXzhtfrgf83brtLdqXii546644ciMPO80tri4JPueQBClKbaomEfoh54ku8E2lw

So the token isn't null.

Am I doing something wrong? Anyone some help?

anitteb
  • 762
  • 10
  • 21
  • I've also tested on different devices, but got the same error. – anitteb Jun 13 '17 at 14:51
  • 4
    Related post: https://stackoverflow.com/q/37549717/4815718 – Bob Snyder Jun 13 '17 at 15:25
  • 15
    **SOLVED**. Thank you very much! The problem was that I wanted to subscribe in _didFinishLaunchingWithOptions_ but in that point not all services were setted up. The solution was to subscribe in the delegate _didRegisterUserNotificationSettings_. – anitteb Jun 14 '17 at 07:53
  • I'm seeing this _sometimes_ on iOS 9.3.5, also confirming token exists. Also calling from didRegisterUserNotificationSettings. – Jonny Sep 06 '17 at 05:29

4 Answers4

6

The problem was that I wanted to subscribe in didFinishLaunchingWithOptions but in that point not all services were set up. The solution was to subscribe in the delegate didRegisterUserNotificationSettings.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
anitteb
  • 762
  • 10
  • 21
3

In MessagingDelegate try it:

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    Messaging.messaging().subscribe(toTopic: "/topics/testTopic")
}
dr OX
  • 4,671
  • 1
  • 17
  • 9
2

I had similar problem. The solution was to invoke FirebaseApp.configure() first:

FirebaseApp.configure()
Messaging.messaging().delegate = self

instead of:

Messaging.messaging().delegate = self // this brakes FCM
FirebaseApp.configure()
kam800
  • 547
  • 5
  • 9
0

The most ideal place to resolve this issue is in the MessagingDelegate method didRefreshRegistrationToken.

func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
   // TODO: subscribe to topics here
}
Scott D
  • 1,394
  • 1
  • 13
  • 17