android - Marshmallow(API 级别 23)Firebase 消息传递不起作用
问题描述
一段时间以来,我一直在尝试在我的应用中实现 Firebase 消息传递。我遵循了不同类型的教程,包括 Firebase 的官方教程,使用 Firebase Assistant 手动和自动添加它。
出于某种原因,以下陈述是正确的:
- 扩展 FirebaseInstanceIdService 的类中的 onTokenRefresh() 永远不会被调用。
- 扩展 FirebaseInstanceIdService 的类中的 onMessageReceived(remoteMessage: RemoteMessage?) 永远不会被调用。
- 没有收到任何消息
MyFirebaseInstanceID 服务:
class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {
val TAG = javaClass?.simpleName
override fun onTokenRefresh() {
super.onTokenRefresh()
val refreshedToken = FirebaseInstanceId.getInstance().token
System.out.println("firebasedebug (" + TAG + "): onTokenRefresh(): " + refreshedToken)
}
}
MyFirebase 消息服务
class : FirebaseMessagingService() {
val TAG = javaClass?.simpleName
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
super.onMessageReceived(remoteMessage)
System.out.println("firebasedebug (" + TAG + "): onMessageReceived(remoteMessage: " + remoteMessage+")")
}
}
我在我的清单中添加了以下内容:
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
<service
android:name="dk.myapp.service.firebase.MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="dk.myapp.service.firebase.MyFirebaseInstanceIDService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
(请注意,我已经尝试将导出设置为 true 和 false 以及带有和不带有用于设置频道的元标记)
我还在我的应用程序类中添加了以下内容:
FirebaseApp.initializeApp(this);
显然,当我尝试通过 Firebase 使用方法“getToken()”时,这是必需的。A 也尝试过使用和不使用这些方法。
在我的主要活动中,我尝试通过 Firebase 放置“getToken()”方法来尝试触发令牌的初始化,但仍然没有任何运气。
FirebaseInstanceId.getInstance().token
我当然已将消息库(和 google play 服务模块)添加到我的应用程序模块 gradle 中:
compile 'com.google.firebase:firebase-messaging:15.0.0'
compile 'com.google.android.gms:play-services-ads:15.0.0'
compile 'com.google.android.gms:play-services-maps:15.0.0'
compile 'com.google.android.gms:play-services-base:15.0.0'
compile 'com.google.android.gms:play-services-location:15.0.0'
An还在gradle文件的底部添加了这一行:
apply plugin: 'com.google.gms.google-services'
这些是我的项目gradle:
buildscript {
ext.kotlin_version = '1.1.4-2'
ext.anko_version = '0.10.2'
repositories {
mavenCentral()
jcenter()
google()
}
dependencies {
...
classpath 'com.google.gms:google-services:3.2.1' // google-services plugin
...
}
allprojects {
tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
}
}
allprojects {
repositories {
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
编辑:我当然也将 google-services.json 添加到项目中
通过 Firebase 仪表板发送消息时,我尝试过: - 输入频道 - 将目标设置为用户段 - 将目标设置为单个设备(来自应用程序的令牌) - ...
我还尝试过以下操作: - 每当我尝试新方法时卸载并重新安装应用程序 - 删除 instanceid 并再次调用 getToken - 在调试和非调试模式下尝试 - ...
这是我的发送消息列表在仪表板中的样子:
如果需要更多详细信息,请告诉我,我当然会提供(如果可能的话)。
编辑 我尝试了 REST API 并做了一些观察。
- 第一次尝试发送标准通知时,我收到了成功:1 和失败:0 的响应,但随后(失败:1)出现“错误”:“未注册”(见下图)
- 我每次都回200。
- 如果我卸载/重新安装应用程序并使用新令牌,则会再次发生与上述相同的事情。
- 没有一次尝试在设备上给我一个可见的推送消息,也没有调用 onMessageReceived 方法,我已经通过代码中的日志消息和 syso 以及调试器对其进行了测试。
解决方案
我终于找到了解决我的特殊情况的方法。
我的解决方案是从应用程序标记中的清单文件中删除以下行:
android:label="@string/app_name"
tools:node="replace"
tools:replace="android:icon,android:label"
推荐阅读
- bash - 为什么 * 将所有文件名放在参数向量中?
- ios - 将 DatePicker 与 UIPickerView 同步并使用 Label 显示从中选择的值
- scala - CrossValidator 调整 spark ML 在模型保存时参数“发现无关参数”失败
- php - PHP毕达哥拉斯检查它是否是正确的答案
- firebase - React-native firebase Signinwithemal 和 createuserwithemailandpassword
- react-native - alpha,beta,生产的不同反应本机应用程序图标?
- python - 为什么我的退出状态为 1?
- python - 用于非缺失值的 pandas groupby
- python-3.x - 为什么我在这个打印语句上得到无效的语法?
- php - Exec() 为FFMPEG创建两个不同的进程