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

基于firebase-admin与Node.js设置收件箱式堆叠通知优先级

解决方案:堆叠通知数据获取与Android优先级配置问题

针对你遇到的两个推送通知问题,我结合实际开发经验整理了可行的解决思路:

问题1:后台/杀死状态下仅能获取最后一条堆叠通知数据

当使用inbox样式的堆叠通知时,FCM默认不会自动汇总所有堆叠通知的自定义数据,加上App在后台/杀死状态下,push.on('notification')通常只会触发最后一条通知的回调,导致丢失历史数据。可以通过以下方式解决:

1. 后端发送时打包所有未读通知数据

每次发送堆叠通知时,将所有未读通知的完整数据(包括新通知和之前的未读通知)以数组形式放入data字段中,确保打开App后能一次性拿到全部数据:

const admin = require('firebase-admin');
// 假设unreadNotifications是用户的所有未读通知数组
let message = {
  token: user.deviceId,
  data: {
    title: notification.title,
    body: status,
    contentAvailable: 'true',
    type,
    targetId,
    // 新增:打包所有未读通知数据
    allNotifications: JSON.stringify(unreadNotifications)
  },
  android: {
    notification: {
      style: 'inbox',
      summaryText: `You have ${unreadNotifications.length} new notifications`,
      // 手动设置堆叠项的显示内容
      inboxStyle: {
        lines: unreadNotifications.map(n => n.shortContent)
      }
    }
  },
  // ... apns配置
}
admin.messaging().send(message)

2. 前端本地存储所有通知数据

在push.on('notification')事件回调中,无论App处于前台还是后台,都将收到的通知数据存储到Ionic的Storage(或本地缓存)中:

import { Storage } from '@ionic/storage';

constructor(private storage: Storage) {}

// 初始化推送监听
push.on('notification', async (data) => {
  // 将通知数据存入本地
  const existingNotifications = await this.storage.get('unreadNotifications') || [];
  existingNotifications.push(data.additionalData);
  await this.storage.set('unreadNotifications', existingNotifications);

  // 处理通知逻辑
  if (data.additionalData.foreground) {
    // 前台处理逻辑
  } else {
    // 后台/点击通知打开App时,从本地读取所有未读数据
    const allUnread = await this.storage.get('unreadNotifications');
    console.log('所有未读通知:', allUnread);
  }
});

即使App被杀死,下次打开时也能从本地存储中读取所有历史通知,不依赖推送事件传递的单条数据。

3. 检查插件版本与配置

确保cordova-plugin-push升级到最新稳定版,旧版本可能存在堆叠通知数据处理的bug。同时确认config.xml中配置了正确的FCM参数:

<plugin name="phonegap-plugin-push" spec="^2.3.0">
  <variable name="SENDER_ID" value="YOUR_FCM_SENDER_ID" />
</plugin>

问题2:Android优先级配置未生效

你当前的android配置为空,需要正确设置优先级参数,同时注意Android 8.0+的通知渠道要求:

1. 后端正确设置Android优先级

在message.android字段中设置消息传递优先级,同时将通知样式参数从data移到android.notification字段(这才是FCM规定的样式配置位置):

let message = {
  token: user.deviceId,
  data: {
    // 只放自定义业务数据
    title: notification.title,
    body: status,
    contentAvailable: 'true',
    type,
    targetId
  },
  android: {
    priority: 'high', // 设置消息传递优先级,确保快速送达
    notification: {
      priority: 'high', // 设置通知显示优先级,触发横幅/提示音
      style: 'inbox',
      summaryText: 'You have %n% new notifications',
      icon: '@mipmap/ic_notification', // 可选:设置通知图标
      sound: 'default' // 可选:设置提示音
    }
  },
  apns: {
    headers: { apnsPriority: '5' } // iOS高优先级对应5
  }
}

2. 前端配置高优先级通知渠道(Android 8.0+)

Android 8.0及以上版本必须创建通知渠道,并设置渠道优先级,否则高优先级通知不会弹出:

const pushOptions = {
  android: {
    senderID: 'YOUR_FCM_SENDER_ID',
    channelId: 'high-priority-channel',
    channelName: 'Important Notifications',
    channelDescription: 'Notifications requiring immediate attention',
    channelImportance: 4, // 4对应IMPORTANCE_HIGH,会弹出横幅
    sound: 'default',
    vibrate: true
  }
};

// 初始化推送
this.push.init(pushOptions).then(() => {
  // 监听通知事件...
});

3. 验证优先级生效

发送测试通知后,检查Android系统的通知设置:确保你的App的"高优先级"渠道已开启通知权限,并且设置为"重要"级别(允许弹出横幅、提示音等)。


内容的提问来源于stack exchange,提问作者Nicolas Ferrari

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 12:39:06