firebase-cloud-messaging - 如何在 Firebase 云功能中指定声音和 click_action
问题描述
我尝试了以下功能(node.js v8):
exports.sendComNotification = functions.firestore
.document('Comunicados/{comID}')
.onUpdate((snap, context) => {
console.log('Com triggered');
const newValue = snap.after.data();
const msg = newValue.title;
var message = {
notification: {
title: 'Comunicado da Diretoria!',
body: msg,
badge: '1',
sound: 'default',
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
topic: "Comunicados"
};
return admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
return
});
});
但这会在功能日志中出现以下错误:
Error sending message: {
Error: Invalid JSON payload received.Unknown name "badge"
at 'message.notification': Cannot find field.
Invalid JSON payload received.Unknown name "sound"
at 'message.notification': Cannot find field.
Invalid JSON payload received.Unknown name "click_action"
at 'message.notification': Cannot find field.
at FirebaseMessagingError.FirebaseError[as constructor](/srv/node_modules / firebase - admin / lib / utils / error.js: 42: 28)
如果我取出“badge”、“sound”和“click_action”,它可以工作,但是接收时没有声音,当然,onResume(flutter 应用程序)中定义的操作也不会触发。设置声音和 click_action 道具的正确方法是什么?提前致谢。
解决方案
好的,我终于设法在这里和那里收集点点滴滴,让它发挥作用。不明白为什么文档没有明确直接的路径来满足这种常见要求。
exports.sendComNotification = functions.firestore
.document('Comunicados/{comID}')
.onCreate((snap, context) => {
const doc = snap.data();
const msg = doc.title;
var message = {
notification: {
title: 'Comunicado da Diretoria!',
body: msg
},
data: {
title: 'Comunicado',
body: msg
},
android: {
notification: {
sound: 'default',
click_action: 'FLUTTER_NOTIFICATION_CLICK',
},
},
apns: {
payload: {
aps: {
badge: 1,
sound: 'default'
},
},
},
topic: "Comunicados"
};
return admin.messaging().send(message)
.then((response) => {
console.log('Successfully sent message:', response);
return
})
.catch((error) => {
console.log('Error sending message:', error);
return
});
});
推荐阅读
- r - 是否有算法/包可以自动修复名单中的错误?
- javascript - 如何为 HTML 数字输入元素设置特定的拒绝消息?
- jquery - JQuery 排除类
- firebase - 查询 Firebase (NoSQL) 数据的子集
- javascript - 如何使用javascript在模态中获取文本框值
- java - 将 favicon 设置为 maven-javadoc-plugin 生成的 javadoc 文件
- udp - 如何从 Indy udp 服务器获取响应字符串?
- ionic-framework - 如何在 Ionic 4 中触发 slideNext
- sql - 在使用 SSIS 从 Oracle 到 MS SQL 的数据迁移期间查找列抛出异常
- javascript - 我是否误解了 React 甚至 Javascript 中“this”的使用?