How to Save a FCM token in Android?

后端 未结 5 1497
一生所求
一生所求 2020-12-01 19:52

I am following this to register my deivce in Firebase

Here I am trying to display and save the notification token

    public class MyFirebaseInstance         


        
相关标签:
5条回答
  • 2020-12-01 20:10

    Just add this in your code..

        public class MyActivity extends AppCompatActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
    
    
            SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
            String token = prefs.getString("token", "");
    
            Log.e("NEW_INACTIVITY_TOKEN", token);
    
            if (TextUtils.isEmpty(token)) {
                FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(MyActivity.this, new OnSuccessListener<InstanceIdResult>() {
                    @Override
                    public void onSuccess(InstanceIdResult instanceIdResult) {
                        String newToken = instanceIdResult.getToken();
                        Log.e("newToken", newToken);
                        SharedPreferences.Editor editor = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE).edit();
                        if (token!=null){
                           editor.putString("token", newToken);
                           editor.apply();
                        }
    
                    }
                });
            }
    
        }
    
    
    }
    

    Firebase Tokens are called once when the app is installed and runned for the first time so ignore the Saving the data or running this Activity/Service once the Data is Saved with Shared Prefs

    0 讨论(0)
  • 2020-12-01 20:20

    Try this just uninstall and reinstall app because token is not generated everytime you play the app. It's generated when you clear sharePref or reinstall.

    0 讨论(0)
  • 2020-12-01 20:24

    No need to Use getToken() new token already returned by onNewToken argument

    @Override
    public void onNewToken(String refreshedToken) {
        // No need to assign refreshedToken with getToken,
        // this method called when token refreshed then use returned refreshedToken directly,
        // use `instanceIdResult.getToken()` described below when you need to use token later throw app usage.
    
        // refreshedToken = FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        storeToken(refreshedToken);
    }
    

    getToken(); is also deprecated

    Get Token in your Activity : .getToken(); is also deprecated if you need to get token in your activity then use as following:

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( MyActivity.this,  new OnSuccessListener<InstanceIdResult>() {
         @Override
         public void onSuccess(InstanceIdResult instanceIdResult) {
               String newToken = instanceIdResult.getToken();
               Log.e("newToken",newToken);
    
         }
     });
    
    0 讨论(0)
  • 2020-12-01 20:25

    May be it takes some time to generate token. If you don't get it you can generate it as follow: (Reference:https://firebase.google.com/docs/cloud-messaging/android/client?authuser=0)

     FirebaseInstanceId.getInstance().getInstanceId()
                        .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() 
     {
                            @Override
                            public void onComplete(@NonNull Task<InstanceIdResult> task) 
       {
                                if (!task.isSuccessful()) {
                                    Log.w(TAG, "getInstanceId failed", 
                                    task.getException());
                                    return;
                                }
    
                                // Get new Instance ID token
                                String token = task.getResult().getToken();
                                //Here you will surely get the token so store it in 
                                //sharedpreference for future use
    
                            }
     });
    
    0 讨论(0)
  • 2020-12-01 20:30

    The onTokenRefresh/onNewToken method will only be called when a new token is generated.

    Quite often (especially during development) your app will already have generated an Instance ID token before you added the service. So onTokenRefresh/onNewToken will not be called, and you won't have a token in your shared preferences.

    For this reason you should get the token directly from your main activity with FirebaseInstanceId.getInstance().getInstanceId() as shown in the documentation. This will pick up the token that was last generated. From there on you use onTokenRefresh/onNewToken to respond to token changes.

    0 讨论(0)
提交回复
热议问题