Firebase (FCM) how to get token

匿名 (未验证) 提交于 2019-12-03 01:25:01

问题:

It`s my first to use FCM.

I download a sample from firebase/quickstart-android and I install the FCM Quickstart.But I can`t get any token from the log even hit the LOG TOKEN button in the app.

Then I try to send message with Firebase console and set to target to my app package name.I got any incoming message.

I want to know can FCM be used?GCM everything is ok.

Solution:

Because I am not a Android developer,just a backend developer.So it takes me some time to solve it.In my opinion,there`re some bug in the sample app.

Code:

RegistrationIntentService.java

public class RegistrationIntentService extends IntentService {      private static final String TAG = "RegIntentService";       public RegistrationIntentService() {         super(TAG);     }      @Override     protected void onHandleIntent(Intent intent) {         String token = FirebaseInstanceId.getInstance().getToken();         Log.i(TAG, "FCM Registration Token: " + token);     } } 

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {      private static final String TAG = "MyFirebaseIIDService";      /**      * Called if InstanceID token is updated. This may occur if the security of      * the previous token had been compromised. Note that this is called when the InstanceID token      * is initially generated so this is where you would retrieve the token.      */     // [START refresh_token]     @Override     public void onTokenRefresh() {         // Get updated InstanceID token. //        String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //        Log.d(TAG, "Refreshed token: " + refreshedToken); // //        // TODO: Implement this method to send any registration to your app's servers. //        sendRegistrationToServer(refreshedToken); //         Intent intent = new Intent(this, RegistrationIntentService.class);         startService(intent);     }     // [END refresh_token]      /**      * Persist token to third-party servers.      * 

* Modify this method to associate the user's FCM InstanceID token with any server-side account * maintained by your application. * * @param token The new token. */ private void sendRegistrationToServer(String token) { // Add custom implementation, as needed. } }

Add this in the MainActivity.java.

 Intent intent = new Intent(this, RegistrationIntentService.class);         startService(intent); 

After do above,you can get the Token in logcat.But finally I find a convenient way to get it.Just use debug mode to install the sample app and you can get the token when you first time to install it.

But I dont why it cant print the log when I install it.Maybe be related to mobile system.

And then why I can`t get the Notification. FirebaseMessagingService.onMessageReceived did not call sendNotification

回答1:

Work for me

Log.d("Firebase", "token "+ FirebaseInstanceId.getInstance().getToken()); 


回答2:

Important information.

if google play service hung or not running, then fcm return token = null



回答3:

According to doc

Migrate a GCM client app to FCM

onTokenRefresh() 

only Called if InstanceID token is updated

So it will call only at first time when you install an app to your device.

So I suggest please uninstall your app manually and try to run again

definitely you will get TOKEN



回答4:

Instead of this:

    // [START refresh_token]     @Override     public void onTokenRefresh() { //        Get updated InstanceID token. //        String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //        Log.d(TAG, "Refreshed token: " + refreshedToken); // //        TODO: Implement this method to send any registration to your app's servers. //        sendRegistrationToServer(refreshedToken); //         Intent intent = new Intent(this, RegistrationIntentService.class);         startService(intent);     }     // [END refresh_token] 

Do this:

    // [START refresh_token]     @Override     public void onTokenRefresh() {         // Get updated InstanceID token.         String refreshedToken = FirebaseInstanceId.getInstance().getToken(); //        Log.d(TAG, "Refreshed token: " + refreshedToken);          // Implement this method to send token to your app's server        sendRegistrationToServer(refreshedToken);      }     // [END refresh_token] 

And one more thing:

You need to call sendRegistrationToServer() method which will update token on server, if you are sending push notifications from server.

UPDATE:

New Firebase token is generated (onTokenRefresh() is called) when:

  • The app deletes Instance ID
  • The app is restored on a new device
  • The user uninstalls/reinstall the app
  • The user clears app data.


回答5:

Try this. Why are you using RegistrationIntentService ?

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {         @Override     public void onTokenRefresh() {             String token = FirebaseInstanceId.getInstance().getToken();             registerToken(token);     }      private void registerToken(String token) {      } } 


回答6:

At the same time don not forget to include this in your manifest file to receive token id



回答7:

This line should get you the firebase FCM token.

String token = FirebaseInstanceId.getInstance().getToken(); Log.d("MYTAG", "This is your Firebase token", + token); 

Do Log.d to print it out to the android monitor.



回答8:

If are using some auth function of FB, you can take token using this:

//------GET USER TOKEN------- FirebaseUser mUser = FirebaseAuth.getInstance().getCurrentUser(); mUser.getToken(true)         .addOnCompleteListener(new OnCompleteListener() {             public void onComplete(@NonNull Task task) {                 if (task.isSuccessful()) {                     String idToken = task.getResult().getToken();                       // ...                 }             }         }); 

Work well if user are logged. getCurrentUser()



回答9:

Settings.Secure.getString(getContentResolver(),                      Settings.Secure.ANDROID_ID); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!