Android Firebase云消息传递(FCM)onTokenRefresh()从未调用过,getToken()返回null

时间:2017-12-22 15:11:39

标签: android firebase firebase-cloud-messaging

我意识到这个问题之前已经提出过,但我已经检查了大量先前的解决方案,并且我在这个问题中提到的问题仍然存在。

这就是我所拥有的:

  1. MyFirebaseInstanceIDService类几乎完全出自本书:

    dependencies {
        implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
    
        implementation 'com.github.bumptech.glide:glide:3.7.0'
    
        implementation 'com.android.support:appcompat-v7:27.0.2'
        implementation 'com.android.support:design:27.0.2'
        implementation 'com.android.support:support-v4:27.0.2'
        implementation 'com.android.support:support-annotations:27.0.2'
        implementation 'com.android.support:palette-v7:27.0.2'
    
        implementation 'com.google.firebase:firebase-core:11.8.0'
        implementation 'com.google.firebase:firebase-auth:11.8.0'
        implementation 'com.google.firebase:firebase-storage:11.8.0'
        implementation 'com.google.firebase:firebase-database:11.8.0'
        implementation 'com.google.firebase:firebase-messaging:11.8.0'
        implementation 'com.firebaseui:firebase-ui-storage:1.0.1'
        implementation 'com.google.android.gms:play-services-ads:11.8.0'
    }
    
    apply plugin: 'com.google.gms.google-services'
    
  2. 在清单(在应用程序子句中)中声明的服务,从Firebase页面复制。另外,我在清单中没有“tools:node =”merge“,这解决了其他人的问题。

    build.gradle
  3. 应用级build.gradle中的所有最新依赖项:

    buildscript {
        repositories {
            jcenter()
            google()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.0.1'
            classpath 'com.google.gms:google-services:3.1.1'  // Added when setting up Firebase
        }
    }
    
    allprojects {
        repositories {
            jcenter()
            google()
            maven {
                url "https://maven.google.com"
                name 'Google'
            }
        }
    }
    
  4. 项目级google-services.json中定义的最新Google服务:

    ACCESS_NETWORK_STATE
  5. 可以肯定的是,我已经将onTokenRefresh再次下载到app文件夹,没有任何效果。最后,我在清单中声明了INTERNET和FirebaseInstanceId.getInstance().getToken()权限。

  6. 过去我有一个类似的应用程序,所以我知道我的设置应该有效。但不知何故,即使卸载/重新安装应用程序,无论是在模拟器中(尝试了几种类型和API 25& 26版本)以及在真实设备上,都不会调用InstanceIDService中的{{1}},并且{{ 1}}在我的应用程序的任何相关类中返回null。

    它似乎也不是与获取令牌相关的计时问题:我在用户登录后立即收到null,并且仍然在签名之前执行。

    我的基本问题是:有没有办法调试/调查令牌问题?在logcat中,我没有看到任何迹象表明存在Firebase问题。

1 个答案:

答案 0 :(得分:0)

我发现问题的发生取决于应用级build.gradle中指定的firebase SDK版本:从SDK版本11.6.0开始不再调用onTokenRefresh。查看发行说明,此版本具有与令牌生成相关的以下更改:

“改进了FCM令牌提取逻辑,以便现在可以更快地创建FCM令牌并减少电池消耗。”

我认为此版本中与FCM相关的更改与我的问题有关,并会发出错误报告。

更新:Firebase支持部门调查了问题,并在AndroidManifest中报告问题的原因是以下几行:

<uses-permission
    android:name="android.permission.WAKE_LOCK"
    tools:node="remove"/>
我曾经添加的

是为了避免请求“防止手机休眠”。如果清单中没有这些行,则刷新FCM令牌时不会与SDK 11.8.0结合使用。

相关问题