Firebase FCM token - When to send to server?

后端 未结 4 812
眼角桃花
眼角桃花 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: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() {
              @Override
              public void onComplete(@NonNull Task 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 .

提交回复
热议问题