In my MainActivity
in my log, I can see the token using FirebaseInstanceId.getInstance().getToken()
and it display the generated token. But it seems like in my MyFirebaseInstanceIDService
where it is extends to FirebaseInstanceIdService
, the onTokenRefresh()
is not called, where in this function it was said that the token is initially generated here. I needed to call sendRegistrationToServer()
that's why I'm trying to know why it doesn't go in the onTokenRefresh()
.
Here is my code
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToServer(refreshedToken);
}
}
in
onCreate
method to delete token.I had the same problem like you. My mistake was the following: I placed my
<service>
tags outside the<application>
tag in the AndroidManifest.xml file.Now mine looks like this:
And it works without any problem.
There are several reasons to not calling the onTokenRefresh methon not called. I have listed that and mention it one by one .
Please make sure you have added the internet permission
check have you added the google-services.json file that generate from google console inside of app directory in your project
4) add the play service class path in project level build.gridle file
5) Make sure you have added those service in the AndroidManifes file inside application tag
6) onToken refresh is called when token refresh and when install the app for the first so make sure you have deleted the app manually or clear the data
7) Make sure you have extends the FirebaseMessagingService in your service class
I have experienced that MyFirebaseInstanceIDService is not called when you are running your application on a device where internet connectivity is not available. In this way onTokenRefresh() is not called. So make sure that your device must has internet connection during running your application for taking updated FirebaseToken.
Also uninstall the previous app & Re-Install it for taking updated token. The registration token change when:
1) The app deletes Instance ID
2) The app is restored on a new device
3) The user uninstalls/reinstall the app
4) The user clears app data.
Make sure you have added
apply plugin: 'com.google.gms.google-services'
this to bottom of the app.gradle file
I had the same problem. My device(kitkat 4.4.2) couldn't get onTokenRefresh() for some reason. My internet connection was perfect, google play services was up to date and all necessary contiditions for firebase to work were met. My code worked perfectly on the devices 5.0.0 and higher. To solve this issue i had to reset my device to factory settings and application started to work. I know it's tough measure but maybe it may help somebody. There must be some problems or conflict with other application, wich caused onTokenRefresh() not to be called. Good luck!