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!
Firebase Cloud Messaging (FCM) operates by generating unique tokens for each device and later using those for sending messages to respective devices.
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.
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.
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With