Firebase FCM token - When to send to server?

后端 未结 4 778
眼角桃花
眼角桃花 2020-12-31 04:19

Okay so I have an app which on first start takes you through a few welcoming slides, then takes you to a login/register page and then to MainActivity.

I

相关标签:
4条回答
  • 2020-12-31 04:20

    Yes FCM token is generated automatically. But try to see this in a different angle.

    This is how I handled it.

    Let FCM generate token as soon as your app starts. OnTokenRefresh will be called and you just save it in your preferences as:

    @Override
    public void onTokenRefresh() {
        // Get updated InstanceID token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG, "Refreshed token: " + refreshedToken);
    
        sendRegistrationToServer(refreshedToken);
    }
    
    private void sendRegistrationToServer(String token) {
        // Add custom implementation, as needed.
        SharedPreferenceUtils.getInstance(this).setValue(getString(R.string.firebase_cloud_messaging_token), token);
        
       // To implement: Only if user is registered, i.e. UserId is available in preference, update token on server.
       int userId = SharedPreferenceUtils.getInstance(this).getIntValue(getString(R.string.user_id), 0);
       if(userId != 0){
           // Implement code to update registration token to server
       }
    }
    

    Hope you are clear with the way. Ask if you need more clearance on it.

    Edit

    Using the new Firebase SDK (21.0.0) , you need to override onNewToken() method instead of onTokenRefresh()

     @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        sendRegistrationToServer(s);
    }
    
    0 讨论(0)
  • 2020-12-31 04:35

    Note that you can always retrieve the token with:

    FirebaseInstanceID.getInstance().getToken();
    

    This will return null if the token has not yet been generated or the token if it has been generated. In your case it is very likely that the token will be generated by the time the user has signed in. So you should be able to send it to your app server as soon as the user has signed in. If it is not available then you would send it in the onTokenRefresh callback as Chintan Soni mentioned.

    Edit

    Using the new Firebase SDK (21.0.0) , you will get your token this way :

     FirebaseInstallations.getInstance().getToken(false).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {
              @Override
              public void onComplete(@NonNull Task<InstallationTokenResult> task) {
                  if(!task.isSuccessful()){
                      return;
                  }
                  // Get new Instance ID token
                  String token = task.getResult().getToken();
    
              }
          });
    

    You better add a listener for more handling on the response .

    0 讨论(0)
  • 2020-12-31 04:38

    We handled it like this:

    1. Our server create/update the token value against a user id (primary key)
    2. Use 2 SharedPreferences
      1. String - token String
      2. Boolean (updated) - whether token is updated on server or not.

    In case of token refresh we update the token string and set the boolean to false. Later whenever user login each time we check for boolean (updated), if that is false - we attach the current token to his id and send it to server and set updated to true.

    0 讨论(0)
  • 2020-12-31 04:41

    December 2020 update : Using the new Firebase SDK (21.0.0) you can get it by overriding onNewToken() method :

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        sendRegistrationToServer(s);
    }
    

    Or by FirebaseInstallations.getInstance() within your scope :

    FirebaseInstallations.getInstance().getToken(false).addOnCompleteListener(new OnCompleteListener<InstallationTokenResult>() {
              @Override
              public void onComplete(@NonNull Task<InstallationTokenResult> task) {
                  if(!task.isSuccessful()){
                      return;
                  }
                  // Get new Instance ID token
                  String token = task.getResult().getToken();
    
              }
          });
    
    0 讨论(0)
提交回复
热议问题