AndroidStudio 中的 FCM 设置 - 工具 - Cloud Messaging 中的 Firebase 助手设置成功.因此,设置已更正,但未生成令牌.通过 MainActivity 中的令牌显示在 logcat 和 Config 类中未生成令牌,但其显示为空.运行问题继续后卸载的应用程序.我也尝试了不同的模拟器,但没有任何解决方案.
FCM setup in AndroidStudio- tools- Firebase Assistant in Cloud Messaging setup to Successfully. So, setup is Corrected but Token is not generated.token is not generated in logcat and Config Class through token display in MainActivity but its display null. Uninstalled app after the run issue continues. I also try different emulator but not any solution.
MyFirebaseInstanceIDService.java
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
@Override
public void onTokenRefresh() {
String newToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + newToken);
Config.TOKEN = newToken;
}
}
AndroidManifest.xml
AndroidManifest.xml
<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=".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.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
build.gradle(项目级)
build.gradle(Project-level)
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.0.0'
}
build.gradle(应用级)
build.gradle(app-level)
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-messaging:10.0.1'
testCompile 'junit:junit:4.12'
compile 'com.squareup.okhttp:okhttp:2.0.0'
}
apply plugin: 'com.google.gms.google-services'
如果需要,您可以在应用中的任何位置使用此代码获取 Firebase 实例 ID 令牌:
At any point in your app, you can use this code to get the Firebase instance ID token if you need it:
String token = FirebaseInstanceId.getInstance().getToken();
您的 FirebaseInstanceIdService 只会在令牌更改时执行.Firebase 文档 指出:
Your FirebaseInstanceIdService will only get executed when the token changes. The Firebase documentation states:
实例 ID 是稳定的,除非:
Instance ID is stable except when:
如果这些事情都没有发生在您的应用上,那么您的 FirebaseInstanceIdService 只会在您的应用安装在设备上时第一次运行时执行.
If none of those things happen to your app, then your FirebaseInstanceIdService will only ever get executed the first time that your app runs when it is installed on a device.
这篇关于Firebase 云消息传递令牌未生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!