如何在 firebase 云函数中指定声音和 click_action
How to specify sound and click_action in firebase cloud function
我尝试使用以下函数 (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 app)中定义的动作没有当然,要么开火。设置声音和 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
});
});
我尝试使用以下函数 (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 app)中定义的动作没有当然,要么开火。设置声音和 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
});
});