首页 > 解决方案 > Firebase API 推送不会在 Android 设备上打开后台应用程序

问题描述

Firebase API 从后端推送有效负载:

 {
  "registration_ids": [
    "IKSiok9Zpip5cDcebQ67wc7TnpDuhMAFJm1xdGGI44s48JbXEu5iYa"
  ],
  "notification": {
    "body": "Push Test Facility Category",
    "title": null,
    "icon": "myicon",
    "sound": "mySound",
    "vibrate": 1,
    "Badge": 1,
    "click_action": "FCM_PLUGIN_ACTIVITY"
  },
  "data": {
    "message": "Push Test Facility Category",
    "title": null,
    "b": "22",
    "id": "2",
    "category_name": "Restaurants",
    "h": "1",
    "p": 1,
    "type": "2"
  }
}

fcm.service.ts

fcmListeners() {
    this.firebase.onNotificationOpen().subscribe(async (data: any) => {

      if (data.tap) {// when user tapped the background notification

      } else { // Received in foreground

      }
    });
  }

你能告诉我为什么当我使用上述有效负载并且应用程序在后台时应用程序没有打开吗?这是 Android 设备上的行为。但在 iOS 设备上没有问题。

如果我使用 Firebase 控制台云消息(即仪表板),那么那里没有问题。即它甚至在安卓设备的后台打开应用程序。有什么线索吗?

注意:我在这里使用firebase 插件

标签: androidangularfirebasefirebase-cloud-messagingionic4

解决方案


问题是该FCM_PLUGIN_ACTIVITY活动可能未在 中定义,AndroidManifest.xml或者如果在此处定义,则可能无法正确实施。

为了修复它,不要click_action在通知的有效负载中指定—— 默认情况下,通知将使用MainActivity并且将打开应用程序。

{
  "registration_ids": [
    ...
  ],
  "notification": {
    ...,
    "click_action": "FCM_PLUGIN_ACTIVITY" // <--- Remove this
  },
  "data": {
    ...
  }
}

推荐阅读