Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Token is always null in Firebase

I am trying to get a token to use cloud messaging in my app with the following simple code :

String token = FirebaseInstanceId.getInstance().getToken();

I have tried to look everywhere on StackOverflow for people who had the same problem and couldn't find any solution... I have, of course, followed the official Google Documentation about the installation (several times) and my token was never generated.

Any idea why I can't get a valid token?

Thanks!

like image 629
fraxool Avatar asked Sep 30 '16 06:09

fraxool


People also ask

Is firebase token unique?

Firebase Cloud Messaging (FCM) operates by generating unique tokens for each device and later using those for sending messages to respective devices.

What is device token in firebase?

This is the token that you must include in targeted send requests from the API, or add to topic subscriptions for targeting topics. As noted in our client setup guides, your app should retrieve this token at initial startup and save it to your app server alongside a timestamp.

Can you change firebase tokens?

Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted. The user is disabled.


2 Answers

OK, I managed to find the solution by myself, it took me a lot of time to figure it out so I thought it would be a good idea to share the solution in case someone gets the same issue.

So the problem came from the fact that I used this line in my AndroidManifest.xml (inside <application> tag) :

tools:node="replace"

This had to be removed or changed by the following :

tools:node="merge"

Otherwise, Firebase will not work in your app. At least, it's how I fixed my app to finally get a valid token!

Hope this will help someone else!

like image 71
fraxool Avatar answered Oct 27 '22 19:10

fraxool


Make Sure that your Firebase "MyFirebaseInstanceIDService.java" file is directly under the package name tree "com.company"

Don't put them in the separate subpackage like "com.company.services". This is because your google-services.json will look for the files directly under "com.company" directory.

Add the following in your manifest application tag below meta tag (if any)

   <service
        android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

You will get your TOKEN on the public void onTokenRefresh() method


PS: Make sure date is correct on your device and having the latest google-services.json from your project console.

like image 26
Dhruv Kaushal Avatar answered Oct 27 '22 18:10

Dhruv Kaushal