You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用关闭(划掉)时FCM纯数据通知无法接收的解决咨询

解决Android应用被划掉后无法接收FCM纯数据通知的问题

1. 修正FCM Payload配置

纯数据消息要在应用关闭状态触发,除了priority: 'high',还得满足两个关键配置:

  • 绝对不能包含notification字段,否则会被系统当成「通知+数据」消息处理
  • 必须添加content_available: true(Android 12+对后台消息触发要求更严格,这个字段是必要项)

示例正确Payload:

{
  "to": "目标设备Token",
  "priority": "high",
  "content_available": true,
  "data": {
    "title": "新消息",
    "content": "这是一条纯数据通知",
    "customKey": "自定义值"
  }
}

2. 配置Expo Headless任务处理后台消息

Expo托管应用在被划掉关闭后,需要通过Headless Task来接收并处理FCM纯数据消息:

  • 项目根目录创建firebase-background-handler.js:
import { firebase } from '@react-native-firebase/messaging';
import notifee from '@notifee/react-native';

export default async function backgroundMessageHandler(message) {
  // 这里可以根据data内容自定义本地通知样式
  await notifee.displayNotification({
    title: message.data.title,
    body: message.data.content,
    android: {
      channelId: 'data-notification-channel',
      groupId: 'data-message-group',
      groupSummary: true,
      smallIcon: 'ic_notification' // 替换成你自己的通知图标
    }
  });
  return Promise.resolve();
}
  • 在app.json中注册这个Headless任务:
{
  "expo": {
    "android": {
      "useNextNotificationsApi": true,
      "headlessTask": [
        {
          "taskName": "firebaseBackgroundMessage",
          "task": "./firebase-background-handler.js"
        }
      ]
    }
  }
}

3. 解决通知样式问题(针对备选的「通知+数据」方案)

如果纯数据方案实在走不通,优化「通知+数据」的样式问题:

  • 生成符合Android规范的通知图标:必须是透明背景、白色前景的矢量图,尺寸覆盖24x24到192x192,放在android/app/src/main/res/drawable-*目录下
  • 提前创建自定义通知通道,指定分组和摘要规则:
import notifee from '@notifee/react-native';

async function initNotificationChannel() {
  await notifee.createChannel({
    id: 'data-notification-channel',
    name: '数据消息通道',
    importance: notifee.AndroidImportance.HIGH,
    groupId: 'data-message-group'
  });
}
  • 发送本地通知时配置分组摘要:
await notifee.displayNotification({
  // ...基础内容
  android: {
    channelId: 'data-notification-channel',
    groupId: 'data-message-group',
    groupSummary: true,
    groupAlertBehavior: notifee.AndroidGroupAlertBehavior.SUMMARY,
    summaryText: '您有{count}条未读消息'
  }
});

4. 绕开Android厂商的后台限制

国内小米、华为、OPPO等厂商对后台进程有严格管控,即使关闭电池优化,划掉应用后仍可能杀死进程:

  • 在应用内添加引导页面,教用户手动开启「自启动权限」「后台弹出界面权限」「关联启动权限」(不同厂商的设置路径不一样,可直接写清楚步骤,比如小米:设置→应用设置→你的应用→权限管理→自启动)
  • 测试时尽量用原生Android设备(而非国内定制ROM)先验证功能是否正常,排除厂商限制的干扰

5. 排查消息送达日志

用Logcat查看FCM消息的接收情况,确认是消息没到设备,还是代码没处理:

adb logcat -s FirebaseMessaging

同时在Firebase控制台的「云消息传递」模块查看消息的送达报告,确认消息是否已成功下发到设备。


内容的提问来源于stack exchange,提问作者Lucas Basquerotto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 00:42:29