【问题标题】:Marshmallow (API Level 23) Firebase messaging is not workingMarshmallow(API 级别 23)Firebase 消息传递不起作用
【发布时间】:2018-05-04 11:37:12
【问题描述】:

一段时间以来,我一直在尝试在我的应用中实现 Firebase 消息传递。我遵循了不同类型的教程,包括 Firebase 的官方教程,手动添加和使用 Firebase 助手自动添加。

由于某种原因,以下陈述是正确的:

  • onTokenRefresh() 在扩展 FirebaseInstanceIdService 的类中,是 从来没有打电话。
  • onMessageReceived(remoteMessage: RemoteMessage?) 在 永远不会调用扩展 FirebaseInstanceIdService 的类。
  • 上没有收到任何消息

MyFirebaseInstanceIDService:

class MyFirebaseInstanceIDService : FirebaseInstanceIdService() {

    val TAG = javaClass?.simpleName

    override fun onTokenRefresh() {
        super.onTokenRefresh()
        val refreshedToken = FirebaseInstanceId.getInstance().token
        System.out.println("firebasedebug (" + TAG + "): onTokenRefresh(): " + refreshedToken)
    }
}

MyFirebaseMessagingService

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 以及调试器对其进行了测试。

【问题讨论】:

  • 您是否已将 google-services.json 文件添加到您的项目中?
  • 您在应用级 build.gradle 文件中包含的 google play 服务版本是什么?你也可以发布那个文件吗?
  • 您是否已将apply plugin: 'com.google.gms.google-services' 添加为模块中的最后一项build.gradle
  • getToken() 本身也返回 null 吗?您是否也尝试过使用 REST API 发送消息?
  • 使用 Firebase 控制台发送 notification 消息负载。如果您要使用 REST API,您可以尝试发送 notification-only 和 data-only 消息负载。如果不知何故,notification-only 消息有效负载使用 REST API 工作,那么问题可能出在 Firebase 控制台,如果不是,那就是其他问题。

标签: android firebase firebase-cloud-messaging android-8.0-oreo


【解决方案1】:

我终于找到了解决我的特殊情况的方法。

我的解决方案是从我的清单文件中的应用程序标记中删除以下行:

android:label="@string/app_name"
tools:node="replace"
tools:replace="android:icon,android:label"

【讨论】:

  • 如果你去掉android:label,那么你将如何设置应用程序的名称?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
相关资源
最近更新 更多