Refresh Firebase Instance ID When Updating Application

In the days of GCM, it was recommended that every time you add an update to your application, you should check if a new registration identifier was specified (since after the update it was not guaranteed that after the update) the application starts.

Is this still the case with FCM? I could not find documentation about this

+3
source share
2 answers

You should check the current token when your application starts and the onTokenRefresh() monitor to determine when the token changes.

Check the current token when starting the application / main action by calling FirebaseInstanceID.getToken() :

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... String token = FirebaseInstanceId.getInstance().getToken(); if (token != null) { sendRegistrationToServer(refreshedToken); } } ... 

The sendRegistrationToServer() method that is called in this snippet is what you implement to send the registration token to your own server (or a serverless solution such as a Firebase database).

Monitoring changes in the token by subclassing FirebaseInstanceIdService and overriding onTokenRefresh() :

 @Override public void onTokenRefresh() { String refreshedToken = FirebaseInstanceId.getInstance().getToken(); // 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); } 

See the documentation here: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register

+3
source

You do not need to check if the token was the last. The onTokenRefresh() method is always called if a new token has been created.

What you need to do is that a check of your token was sent to your server when calling onTokenRefresh() .

0
source

Source: https://habr.com/ru/post/1268956/


All Articles