首页 > 解决方案 > 尽管设备已注册且 API 显示成功,如何解决 Android 不显示 Firebase 推送通知的问题

问题描述

我有一个使用https://github.com/zo0r/react-native-push-notification来管理推送通知的 React Native 应用程序。尝试在 Android 上从 GCM 迁移到 FCM 消息不起作用,我找不到调试问题的方法。

我有:

AndroidManifest.xml 相关部分:

<manifest...>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <application...>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_name"
                android:value="CHANNEL NAME"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_channel_description"
                    android:value="CHANNEL DESCRIPTION"/>
        <meta-data  android:name="com.dieam.reactnativepushnotification.notification_color"
                    android:resource="@android:color/white"/>
        <activity...>
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationPublisher" />
        <receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationRegistrationService"/>

        <service
            android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>

app/build.gradle 包括:

dependencies {
...
    implementation "com.google.firebase:firebase-core:16.0.1"
    implementation "com.google.firebase:firebase-messaging:17.5.0"
}
apply plugin: 'com.google.gms.google-services'

build.gradle 包括:

buildscript {
...
    dependencies {
        classpath 'com.google.gms:google-services:4.0.1'
        classpath 'com.android.tools.build:gradle:3.0.0'
    }
...
}

google-services.json 已正确放置并具有匹配的包名称。

https://fcm.googleapis.com/fcm/send
POST 请求(ID 已删除)

Header "Authorization: key=AUTH_KEY"
{
  "to": "DEVICE_TOKEN",
  "notification":
    {
       "title": "Test",
       "body": "Test"
    },
  "priority": 10
}

响应(已删除 ID)

{
  "multicast_id": MULTICAST_ID,
  "success": 1,
  "failure": 0,
  "canonical_ids": 0,
  "results":
  [
    {
      "message_id": "0:MESSAGE_ID"
    }
  ]
}

向应用发送测试消息时的 Firebase 控制台显示:

This campaign targets 1 app user(s) (100% of potential users)

表示设备已注册,可以发送活动,但从不显示在设备上。

如何确定消息未显示在设备上的原因?

标签: androidfirebasereact-nativepush-notificationfirebase-cloud-messaging

解决方案


在您的帖子请求中添加一个数据字段,例如:

Header "Authorization: key=AUTH_KEY"
{
"to": "DEVICE_TOKEN",
"notification":
{
   "title": "Test",
   "body": "Test"
},
"data":{
   "body": "Test"
},
"priority": 10
}

而且,您需要在 RNPushNotificationListenerService 中覆盖 onMessageReceived。

这样做,您应该能够处理 onMessageReceived 中的通知,而不管应用程序是在后台还是前台。

请参阅:在 Firebase 中处理消息


推荐阅读