首页 > 解决方案 > Marshmallow(API 级别 23)Firebase 消息传递不起作用

问题描述

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

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

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 并做了一些观察。

在此处输入图像描述

标签: androidfirebasefirebase-cloud-messagingandroid-8.0-oreo

解决方案


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

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

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

推荐阅读