I have initiated MyFirebaseInstanseIdService
in order to get token
, directly i will show you the code :
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
storeRegIdInPref(refreshedToken);
sendRegistrationToServer(refreshedToken);
Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
registrationComplete.putExtra("token", refreshedToken);
LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
}
private void sendRegistrationToServer(final String token) {
// sending gcm token to server
Log.e(TAG, "sendRegistrationToServer: " + token);
}
private void storeRegIdInPref(String token) {
SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
SharedPreferences.Editor editor = pref.edit();
editor.putString("regId", token);
editor.commit();
}
My manifest.xml :
<service android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".services.MyFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
I tried to get token via broadCast in my activity also with shared prefrences, but it seems this class didn't invoked.
In my log
i see those lines :
D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
and this
I/FirebaseInitProvider: FirebaseApp initialization successful
The onTokenRefresh()
method only gets called when the current token expires. The current token could expire for the following reasons:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
There's also the scenario where the first time you call getToken()
, it might return a null
value, from where you should expect onTokenRefresh()
to trigger.
With that, you should call getToken()
on your initial activity to generate a token.