简体   繁体   English

从未调用过Android Firebase Cloud Messaging(FCM)onTokenRefresh()并且getToken()返回null

[英]Android Firebase Cloud Messaging (FCM) onTokenRefresh() never called and getToken() returning null

I realize this question was raised before, but I have checked a substantial list of previous solutions and my problems mentioned in the subject remain. 我意识到以前曾提出过这个问题,但是我检查了以前解决方案的大量清单,而我在本主题中提到的问题仍然存在。

Here's what I have: 这是我所拥有的:

  1. MyFirebaseInstanceIDService class is almost straight out of the book: MyFirebaseInstanceIDService类几乎完全是本书中的内容:

     import android.util.Log; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceIdService; public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { private static final String TAG = "MT: FIIDService"; @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); sendRegistrationToServer(refreshedToken); } private void sendRegistrationToServer(String token) { FirebaseAuth mAuth = FirebaseAuth.getInstance(); FirebaseUser user = mAuth.getCurrentUser(); if (user != null) { FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference IDTokensRef = database.getReference(getString(R.string.firebase_idtokens)); IDTokensRef.child(user.getUid()).setValue(token); Log.d(TAG, "Refreshed token assigned to user: " + user.getDisplayName()); } } 
  2. The service declared in the manifest (within the application clause), copied from the Firebase pages. 从Firebase页面复制的清单中声明的​​服务(在application子句中)。 Also, I have no "tools:node="merge" in the manifest, which solved the issue for some other people. 另外,清单中没有“ tools:node =“ merge”,这为其他人解决了该问题。

     <service android:name=".MyFirebaseInstanceIDService"> <intent-filter> <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> </intent-filter> </service> 
  3. All the latest dependencies in the app level build.gradle: 应用程序级别build.gradle中的所有最新依赖项:

     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' 
  4. The latest google services defined in the project level build.gradle : 在项目级别build.gradle定义的最新google服务:

     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' } } } 
  5. To be sure, I have downloaded the google-services.json once again to the app folder, without any effect. 可以肯定的是,我已经再次将google-services.json下载到app文件夹中,没有任何效果。 Finally, I declare INTERNET and ACCESS_NETWORK_STATE permissions in the manifest. 最后,我在清单中声明了INTERNET和ACCESS_NETWORK_STATE权限。

I have had a similar app working in the past, so I know my setup should work. 过去我有一个类似的应用程序可以工作,所以我知道我的设置应该可以工作。 But somehow, even after uninstalling/reinstalling the app, both in the emulator (tried several types and API 25 & 26 versions) as well as on a real device, onTokenRefresh in the InstanceIDService is never called, and FirebaseInstanceId.getInstance().getToken() returns null, in any relevant class of my application. 但是无论如何,即使在模拟器中(尝试了几种类型以及API 25和26版本)以及在真实设备上卸载/重新安装应用程序之后,也永远不会调用InstanceIDService中的onTokenRefresh以及FirebaseInstanceId.getInstance().getToken()在我的应用程序的任何相关类中返回null。

It also does not seem to be a timing issue related to obtaining a token: I receive null both immediately after user signs on, and still, do just before signing off. 这似乎也不是与获取令牌有关的时间问题:我在用户登录后立即收到null,但仍然在退出之前收到null。

My basic question is: are there ways to debug/investigate the token problem? 我的基本问题是:有没有办法调试/调查令牌问题? In logcat I do not see any indications that there is a Firebase problem. 在logcat中,我看不到任何迹象表明存在Firebase问题。

I have found out that occurrence of the problem depends on the firebase SDK version that is specified in the app level build.gradle : onTokenRefresh is no longer called as of SDK version 11.6.0. 我已经找到了问题的发生取决于在应用程序级别指定的火力SDK版本build.gradleonTokenRefresh不再称为的SDK版本11.6.0。 Looking at the release notes, this version has the following change related to token generation: 查看发行说明,此版本具有与令牌生成相关的以下更改:

"Improved FCM token fetch logic so that the FCM token is now created faster and with less battery drain." “改进了FCM令牌提取逻辑,因此现在可以更快地创建FCM令牌,并减少电池消耗。”

I assume that the FCM related changes in this release are related to my problem, and will issue a bug report. 我认为此版本中与FCM相关的更改与我的问题有关,并且将发布错误报告。

Update : Firebase Support investigated the problem and reported back that the origin of the problem was the following lines in my AndroidManifest: 更新 :Firebase支持人员调查了该问题,并报告了该问题的根源是我的AndroidManifest中的以下几行:

<uses-permission
    android:name="android.permission.WAKE_LOCK"
    tools:node="remove"/>

which I once added to avoid requesting "prevent phone from sleeping permission". 我曾经添加过,以避免请求“防止手机进入睡眠状态”。 Without these lines in the manifest the FCM token is refreshed without issues in combination with SDK 11.8.0. 如果清单中没有这些行,则与SDK 11.8.0结合使用时,FCM令牌将被刷新而不会出现问题。

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

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