【问题标题】:Firebase Cloud Messaging token is not generatingFirebase 云消息传递令牌未生成
【发布时间】:2017-10-30 11:07:11
【问题描述】:

AndroidStudio 中的 FCM 设置 - 工具 - Cloud Messaging 中的 Firebase 助手设置成功。因此,设置已更正但未生成令牌。通过 MainActivity 中的令牌显示在 logcat 和 Config 类中未生成令牌,但其显示为空。运行问题继续后卸载的应用程序。我也尝试了不同的模拟器,但没有任何解决方案。

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

<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(项目级)

  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(应用级)

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'

【问题讨论】:

  • 卸载应用并重试...
  • 您的应用目录下是否有 googleservies.json 文件?
  • 是的,app目录下的googleservies.json文件

标签: android push-notification firebase-cloud-messaging


【解决方案1】:

如果需要,您可以在应用中的任何位置使用此代码获取 Firebase 实例 ID 令牌:

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

您的 FirebaseInstanceIdService 只会在令牌更改时执行。 The Firebase documentation 状态:

实例 ID 是稳定的,除非:

  • 应用删除实例 ID
  • 应用已在新设备上恢复
  • 用户卸载/重新安装应用程序
  • 用户清除应用数据

如果这些事情都没有发生在您的应用上,那么您的 FirebaseInstanceIdService 只会在您的应用安装在设备上时第一次运行时执行。

【讨论】:

    【解决方案2】:

    不会在每次运行应用程序时调用 onTokenRefresh() 方法。只有在生成新令牌时才会调用它。所以很少被调用。

    但它总是在第一次运行时被调用。所以在 onTokenRefresh() 方法中,你应该将刷新后的令牌保存在共享首选项中。

    因此,当您需要 firebase 令牌时,您应该从始终可用的共享首选项中获取它。

    现在您可以通过清除应用数据或卸载并重新安装应用来修复它,以便生成新的令牌。

    【讨论】:

    • 第一次没有生成token。如何第一次生成。我使用了共享偏好,但令牌显示为空。
    【解决方案3】:
     <service android:name=".MyFirebaseMsgService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
    

    将此添加到清单

    【讨论】:

      【解决方案4】:

      在您的清单 Firebase 服务中添加 &lt;action android:name="com.google.firebase.INSTANCE_ID_EVENT"/&gt;

      <service
                      android:name=".common.firebase.FirebaseMessagingService"
                      android:exported="false">
                      <intent-filter>
                          <action android:name="com.google.firebase.MESSAGING_EVENT" />
                          <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                      </intent-filter>
                  </service>
      

      【讨论】:

        【解决方案5】:

        对于大于 18.0.0 FirebaseInstanceId.getInstance().getToken(); 的 firebase 版本已弃用。

        现在我们必须使用以下方法以编程方式获取令牌。

         FirebaseMessaging.getInstance().token.addOnCompleteListener {
                            if (!it.isSuccessful) {
                                return@addOnCompleteListener
                            }
                            
                            val token = it.result
                        }
        

        【讨论】:

          猜你喜欢
          • 2021-04-14
          • 2019-07-19
          • 1970-01-01
          • 2016-11-21
          • 2017-07-04
          • 1970-01-01
          • 2020-12-10
          • 2022-12-21
          相关资源
          最近更新 更多