In what period does the firebase's app token changes and how to manage it?

后端 未结 3 585
梦谈多话
梦谈多话 2020-11-29 04:10

I am new to firebase I am learning it like a toddler learning to walk. So far I have managed to send a message to my own phone using a token generated for my phone by fireba

3条回答
  •  伪装坚强ぢ
    2020-11-29 04:37

    • onTokenRefresh() and FirebaseInstanceIdService are deprecated.
    • This call is also deprecated FirebaseInstanceId.getInstance().getToken()

    Instead, You should override onNewToken(String token) in FirebaseMessagingService. This method triggered when the token is changed. Once you override this method, you can safely remove FirebaseInstanceIdService whcih contains onTokenRefresh().

    When token can change?

    • App deletes Instance ID
    • App is restored on a new device
    • User uninstalls/reinstall the app
    • User clears app data

    How to retrieve the current token:

    by calling FirebaseInstanceId.getInstance().getInstanceId():

    FirebaseInstanceId.getInstance().getInstanceId()
        .addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (!task.isSuccessful()) {
                    Log.w(TAG, "getInstanceId failed", task.getException());
                    return;
                }
    
                // Get new Instance ID token
                String token = task.getResult().getToken();
    
                // Log and toast
                String msg = getString(R.string.msg_token_fmt, token);
                Log.d(TAG, msg);
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    

    For more info: https://firebase.google.com/docs/cloud-messaging/android/client

    For Managing tokens for specific sender id (other than the default sender id), check here

提交回复
热议问题