Reputation: 9179
I'm using this well known code to save a new firebase token on my server (from: Retrieve the current registration token):
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
The problem is, that if the user installs the app on some other device, the same code will be executed and the former firebase token will be overridden by the new one.
So I have to distinguish between the devices, but how can I do it? Device name is for sure not unique enough for this.
EDIT: unfortunately, the suggestion by Bob Snyder did not work. The FirebaseInstanceId#getId() is always different if I remove the App data (cache) or reinstall the app.
Upvotes: 2
Views: 1308
Reputation: 38289
The token is unique to each device and includes the app instance ID. The 11-character instance ID, which is the same one returned by FirebaseInstanceId.getInstance().getId(), appears first, followed by a colon, and the remainder of the token. Example:
eiURDSe_P4q:APA91bFvtCzK...LRpzvQOSdNKioklO
You can use the instance ID as a unique key to store the token on the server.
Upvotes: 2