问题
I'm new to android and now looking on firebase.In my app multiple mobile numbers can be logged in. So what am doing is deleting current token by
FirebaseInstanceId.getInstance().deleteInstanceId();
and after that when I log in with new number, then new token gets generated.
So my question in this exactly which event triggers token regeneration event. One more thing that above code line should I have to run on thread (other than MAIN THREAD) to work
回答1:
This is the service which I used for getting firebase token
public class FCMInstanceIDListenerService extends FirebaseInstanceIdService {
AppSharedPreferences appSharedPreferences;
@Override
public void onCreate() {
super.onCreate();
String CurrentToken = FirebaseInstanceId.getInstance().getToken();
if (CurrentToken!=null){
Intent intent = new Intent("device_id");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.d("token", "Refreshed token: " + CurrentToken);
appSharedPreferences.putString("device_id",CurrentToken);
}
else {
onTokenRefresh();
}
}
public FCMInstanceIDListenerService() {
appSharedPreferences=AppSharedPreferences.getsharedprefInstance(this);
// prefManager = PrefManager.getInstance(this);
}
@Override
public void onTokenRefresh() {
super.onTokenRefresh();
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Intent intent = new Intent("device_id");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.d("token", "Refreshed token: " + refreshedToken);
appSharedPreferences.putString("device_id",refreshedToken);
// prefManager.putString(PrefrenceConstants.KEY_DEVICE_ID, refreshedToken);
}
}
From developer site:
onTokenRefresh() Called when the system determines that the tokens need to be refreshed. The application should call getToken() and send the tokens to all application servers.
This will not be called very frequently, it is needed for key rotation and to handle Instance ID changes due to:
App deletes Instance ID
App is restored on a new device
User uninstalls/reinstall the app
User clears app data
The system will throttle the refresh event across all devices to avoid overloading application servers with token updates.
来源:https://stackoverflow.com/questions/43067655/when-do-fcm-refresh-tokens-created