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);
}
}
I've been struggling with that for over an hour, then I changed the following code and it suddenly worked.
handle your refreshedToken as such:
Try adding android:exported="true"> to both MyFirebaseMessagingService and MyFirebaseInstanceIDService in manifest so it looks like that:
Uninstall your app, Install again, worked. Tested on real device.
android:exported="true" is a default option so you can also remove that entirely and it will be set to true.
Also if you want to call onTokenRefresh more explicitly, you can simply call that anywhere in your code:
You don't have to rely on broadcast being received any more