如何生成Firebase注册令牌 [英] How to generate firebase registration token
问题描述
我正在尝试使用新方法生成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>
以上方法在日志猫中没有显示令牌.请让我知道我在做什么错.任何帮助将不胜感激.
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注册令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!