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();
See the documentation here: https://firebase.google.com/docs/cloud-messaging/android/client#sample-register
source share