我正在尝试使用新方法生成 Firebase 注册令牌,但无法生成这是我的以下代码:
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>
使用上述方法在 log cat 中没有显示任何令牌.请有人告诉我我做错了什么.任何帮助将不胜感激.
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.
谢谢
如果您只想要 fcm 令牌(不关心通知),您只需要清单中的以下服务和操作 INSTANCE_ID_EVENT...
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>
然后在您的活动中添加以下代码(onCreate 会很好)
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.
这篇关于如何生成 Firebase 注册令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!