Creating Firebase Instance ID when opening the app for the first time

1.4k views Asked by At

So I am having a hard time figuring out when the InstanceID is created any why it isn't working upon the first time it is opened.

When I run the app for the first time, this code returns null.

FirebaseInstanceId.getInstance().getToken();

I even tried a while loop and just waited till it returned something but no luck.

BUT when I close the app and open it again, I then get the InstanceID. Any clue why? I'm trying to obtain it on the splash screen of the app.

onTokenRefresh is not being called untill the restart. I can see some work arounds... but prefer to just get it off the bat.

Thanks!

2

There are 2 answers

1
Doug Stevenson On

From the documentation:

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseInstanceIdService.

You need to subclass FirebaseInstanceIdService, add that service to your manifest, and override onTokenRefresh() to get notified of any updates to the token.

If FirebaseInstanceId.getInstance().getToken() returns null, that means there's just no token available yet. So you should be waiting for it instead in your service.

0
Daniel Choi On

Turns out, It washing saving properly rather than it not being created. It was one of those days..