Token is always null in Firebase

2019-04-07 13:55发布

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!

2条回答
Summer. ? 凉城
2楼-- · 2019-04-07 14:53

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!

查看更多
SAY GOODBYE
3楼-- · 2019-04-07 14:53

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.

查看更多
登录 后发表回答