How to Save a FCM token in Android?

后端 未结 5 1502
一生所求
一生所求 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: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() 
     {
                            @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();
                                //Here you will surely get the token so store it in 
                                //sharedpreference for future use
    
                            }
     });
    

提交回复
热议问题