Token update in FCM - android

I am using FCM in my project and my application is register with two FCM project (two sender ID).
Now i want to handle token refresh.And as per document i will get below call back
#Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
}
but have may i know which token is updated ?
token updated for sender_id_1 or token updated for sender_id_2 ?
Question 2 : If i receive onNewToken call back with newToken value and if i do not pass that value to server and server try to send push on older token, then what will happen ? what error i will received from FCM ?
Thanks

Looking at the docs for onNewToken() (emphasis mine):
Called when a new token for the default Firebase project is generated.
This is invoked after app install when a token is first generated, and again if the token changes.
Default project points to the first project.
Sending to an expired token would result to a NotRegistered error

Related

Firebase Cloud Messaging : Expiration of FCM token

I understand that the FCM token renews itself if one of the following happens.
-The app deletes Instance ID
-The app is restored on a new device
-The user uninstalls/reinstall the app
-The user clears app data.
The following can be used at the App side to monitor Token renewal.
Monitor token generation
The onTokenRefreshcallback fires whenever a new token is generated, so
calling getToken in its context ensures that you are accessing a
current, available registration token. Make sure you have added the
service to your manifest, then call getToken in the context of
onTokenRefresh, and log the value as shown:
#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);
}
My question is, when the app is terminated, the token expires and there is no way for the FCM server to know what the new token is for the device (if exist). So when I send a notification/data message to this device, the server fails to send it to the device as it doesn't know where to send it to (as there is no valid token). How do I make sure that in such situations I can notify the device ? I dont do a customer token generation. So the it seems to refresh the token now and then. How do I increase the validity of my token ?
You will need to check for an error when sending the message, and pay attention to the error codes, as listed in the documentation. You should stop using the token if you get the error messaging/registration-token-not-registered.

How to store firebase token on my server if user have more than one device

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.
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.

Google Firebase token not NotRegistered with Android

I have problem with Firebase, i get the token with FirebaseInstanceIdService service with method onTokenRefresh
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.i(TAG, "Refreshed token: " + refreshedToken);
}
I take the from the Android Studio Logcat( select it and copy/paste ) and i paste Postman tool.
With some token I don't have problem but with other I have the error
NotRegistered
Example of token notRegistered:
e98tZENV-FY:APA91bEBScMMWJSDtU08ZaXy172KPSle_dC5TFPUHiZ_OCg6o03Jmk0yi288v3byLVfLVUkksGJ7m-QR2swMUPlMlmwFiPWswLrKd9UTbtdbx0l02k0OhT84YAC0QLz6UCyhsn-poLwp
So some things that are sure ( because some token are sending and same app, same server, same android studio, etc):
The Server Key is ok.
The Google Service Json is ok.
The token is copy/paste correctly
Can be some issue with some special character or a bug of Firebase?
Thanks!!!

Getting GCM Registration ID using Firebase

I have an ASP.net MVC server from where I want to send push notifications to my Android App. I have already implemented Firebase Messaging in the app and the notifications are working fine when sent from Firebase dashboard.
I have tried sending push notification using the server by sending a post request, but the request requires a to field. Earlier we used to send registration id provided by GCM there. Now since Firebase is handling it, how can I fetch the registration id to be put in the to field using Firebase SDK in Android?
Hi and thanks for using Firebase Cloud Messaging!
You can fetch the registration-id calling:
FirebaseInstanceId.getInstance().getToken();
Differently from the GCM sdk, the new library automatically takes care of fetching the token as soon as possible, and then it caches it locally.
The above method will return the token, if available, or null if the fetching phase is still in progress.
You can use the callback onTokenRefresh() to be notified when the token is available or has been rotated.
public class InstanceIDService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
sendRegistrationToMyServer(refreshedToken);
}
}
More info here: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

Google Cloud Messaging for Cordova Clarification

I am implementing Google Cloud Messaging service in my cordova app. so far everything is working well. I however have some few issues bothering I wish someone can clarify them for me.
1) At this section of the code where I get the device GCM regID and further save it to on my server. I will like to know if I should call this script and thus save GCM regID to my server anytime the user opens the App or it should be called and saved once..
function onNotification(e) {
switch( e.event )
{
case 'registered':
if ( e.regid.length > 0 )
{
console.log("regID = " + e.regid);
}
break;
}
}
2) I have noticed on the console that my registered ID sometime changes. I will like to know why that happens, whether it is normal and also if I should be updating the users GCM regID on my server.
I wil be glad if anyone can clarify these for me. Thank you
for (1) it should be called and saved on server once. as am working in my applications and save this id to server once, and all things working fine.
for (2) however in development the registrations id's sometimes changes. The reason is that in development we uninstall or reinstall the application completely. thus makes the registration id's to change.because registration id assigned on app installation. but for exceptional case also see this.
You should call the script every time. Only update the registration ID if it has changed.
Read my answer. Also, this answer for more details
You only need to send your registration token to your server once. If your registration token changes you should send the changed registration token to your server.
The registration token may change if the application is uninstalled and reinstalled or if GCM determines that the token has been compromised in some way.
You should register a service to listen for registration token changes it should extend InstanceIDListenerService and you override its onTokenRefresh method like this:
#Override
public void onTokenRefresh() {
// Fetch updated Instance ID token and notify our app's server of any changes (if applicable).
InstanceID instanceID = InstanceID.getInstance(this);
String token = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
Log.i(TAG, "GCM Registration Token: " + token);
// Send token to server.
}
Consider GCM getting started documentation for more details.

Categories

Resources