How to generate firebase registration token

守給你的承諾、 提交于 2019-12-31 04:45:10

问题


I am trying to generate firebase registration token with new method but unable to generate this is my code below:

MyFirebaseInstanceIdService.java

public class MyFirebaseInstanceIdService extends FirebaseMessagingService {

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

    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {

            String token = instanceIdResult.getToken();
            Log.d("Token",token);
        }
    });
  }
}

Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.app.retrofitapp">

<dist:module dist:instant="true" />

<uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".Users"></activity>

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".MyFirebaseInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
    </intent-filter>
    </service>

</application>

There is no token is showing in log cat with above method.Someone please let me know what I am doing wrong.Any help would be appreciated.

THANKS


回答1:


If you want just fcm token(not bother about notification) you only need below service in manifest with action INSTANCE_ID_EVENT...

<service android:name=".fcm.FireBaseInstanceIdService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

And after this add the below code in your activity (onCreate will be good)

FirebaseInstanceId.getInstance().getInstanceId()
                .addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
                    @Override
                    public void onComplete(@NonNull Task<InstanceIdResult> task) {
                        if (!task.isSuccessful()) {
                            Log.w(TAG, "Failed", task.getException());
                            return;
                        }

                        String token = task.getResult().getToken();
                        Log.i("FCM", "Current token=" + token);
                    }
                });

You will get your current token there. Hope it will help.




回答2:


You can directly Log the token into your onNewToken method of FireBaseMessagingService class...

@Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.i("FCM", "FCMMessagingService token=" + s);
        //store it in prefrence
        ApplicationPreferences.setFcmToken(getApplicationContext(), s);
    }

No need to call...(this method is useful when you want token in other class/activity)

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {

            String token = instanceIdResult.getToken();
            Log.d("Token",token);
        }
    });

Confirm two services in manifest file

        <service android:name=".fcm.FireBaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            </intent-filter>
        </service>
        <service android:name=".fcm.FireBaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>



回答3:


In your case this is giving the token, try debugging on the real device.

 @Override
   public void onNewToken(String token) {
    Log.d(TAG, "Refreshed token: " + token);

   }



回答4:


Add below line in your Application class or in launch activity.

Without initialising your firebase instance token will never get generated.

    FirebaseApp.initializeApp(getApplicationContext());



回答5:


@Override    
  public void onNewToken(String token) {   
    Log.d(TAG, "Refreshed token: " + token);
    Note - here you get the firebase token everytime. and you can save firebase token into your app or with your server.
  }


来源:https://stackoverflow.com/questions/56625061/how-to-generate-firebase-registration-token

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