如何在云函数FCM中正确指定channel_id解决通知静默问题
修复步骤
- 补充云函数FCM消息的安卓优先级配置,在
android节点下新增priority: "high",同时在android.notification下新增importance: "high",明确指定通知的展示优先级,修改后代码示例:
const message = { "notification": { "title": "Message Received", "body": "Text Message from " + fromUserName, }, "tokens": registrationTokens, "android": { "priority": "high", "notification": { "channel_id": "high_importance_channel", "importance": "high" }, }, };
- 清理历史无效的通知渠道配置:如果之前曾用
high_importance_channel这个ID创建过低优先级渠道,后续修改创建参数不会覆盖系统已保存的渠道配置。可以先卸载APP重装,或者在创建渠道前先调用deleteNotificationChannel方法删除旧渠道,再重新创建当前的高优先级渠道。 - 检查安卓通知权限:安卓13及以上版本需要APP主动动态申请
POST_NOTIFICATIONS权限,未获得授权的情况下所有通知都会被系统强制设置为静默状态。 - 修正云函数异步逻辑:当前云函数使用
async声明,但admin.messaging().sendMulticast方法前未加await,会导致云函数在消息发送完成前就被系统回收,可能出现参数传递不全的问题,修改为:
await admin.messaging().sendMulticast(message) .then((response) => { console.log(response.successCount + ' messages were sent successfully'); });
内容的提问来源于stack exchange,提问作者BJW
相关产品推荐
相关产品推荐

