简体   繁体   English

未生成 FCM 推送通知令牌

[英]FCM Push Notification token is not Generated

I have create Project on firebase console site.我在 firebase 控制台站点上创建了项目。 Project is successfully created and download google-service.json file and paste in app on android studio.项目已成功创建并下载 google-service.json 文件并粘贴到 android studio 上的应用程序中。 created class FirebaseInstanceIDService.java token generated in display logcat but it not generated and not display.在显示 logcat 中生成的创建类 FirebaseInstanceIDService.java 令牌但未生成且未显示。 when project run debug mode display FirebaseInstanceIDService.java is not load.当项目运行调试模式显示 FirebaseInstanceIDService.java 未加载时。

Android Studio tools - Firebase Assistant Cloud messaging setup Correctly display and connection also correctly display but token is not generated. Android Studio 工具 - Firebase 助手云消息设置正确显示和连接也正确显示但未生成令牌。

FirebaseInstanceIDService.java FirebaseInstanceIDService.java

public class FirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

    @Override
    public void onTokenRefresh() {
          super.onTokenRefresh();

        String newToken = FirebaseInstanceId.getInstance().getToken();

        Log.d(TAG, "Refreshed token: " + newToken);

        Constan.token = newToken;
    }
}

Gradle.build(Project-level) Gradle.build(项目级)

     dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0'
            classpath 'com.google.gms:google-services:3.1.1'
}

Gradle.build(app-level) Gradle.build(应用级)

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:11.0.4'
    testCompile 'junit:junit:4.12'

    compile 'com.squareup.okhttp:okhttp:2.0.0'
}

apply plugin: 'com.google.gms.google-services'

AndroidManifest.xml AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.firebase.zala.fiirebase">

    <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=".FirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>


    </application>

</manifest>

uninstall you app from phone , and then again install and set your logcat to debug.从手机上卸载您的应用程序,然后再次安装并将您的 logcat 设置为调试。 you'll get your token.你会得到你的令牌。

The token will not be refreshed every time you run the app.每次运行应用程序时都不会刷新令牌。 So the function onTokenRefresh will rarely be called.所以onTokenRefresh函数很少会被调用。

But it will surely be called the first time you run the app.但它肯定会在您第一次运行应用程序时被调用。 So when it is obtained, you should save it in the shared preferences so that in next usages of the app, you can get the token from the shared preferences.所以获取的时候,应该保存在共享首选项中,以便下次使用应用时,可以从共享首选项中获取token。

To get the token again, you can try uninstalling the app or clearing the app data, so that a new token will be generated and the method onTokenRefresh will be called.再次获取token,可以尝试卸载app或者清除app数据,这样会生成一个新的token,调用onTokenRefresh方法。

Please go through to thislink for FCM integration.请转到此链接以进行 FCM 集成。

1) maven entry in build.gralde(project level) 1) build.gralde中的maven入口(项目级)

    allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }// Google's Maven repository
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM