Marshmallow(API 级别 23)Firebase 消息传递不起作用

Marshmallow (API Level 23) Firebase messaging is not working

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

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

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,以及使用和不使用用于设置频道的元标记)

我还在我的申请中添加了以下内容class:

FirebaseApp.initializeApp(this);

显然,当我尝试通过 Firebase 使用方法 "getToken()" 时,这是必需的。 A 也尝试过使用和不使用这些方法。

在我的主要 activity 中,我尝试通过 Firebase 放置 "getToken()" 方法来尝试触发令牌的初始化,但仍然没有成功。

FirebaseInstanceId.getInstance().token

我当然已经将消息传递库(和 google 播放服务模块)添加到我的应用程序模块 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 并进行了一些观察。

我终于找到了适合我的特殊情况的解决方案。

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

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