【发布时间】:2020-08-12 02:35:29
【问题描述】:
我正在尝试在用户收到消息时向他们发送推送通知,通知成功并显示横幅,但它是静音且不振动的。
我使用 Atom 作为服务器来发送通知,代码看起来像,
exports.observeMessages = functions.database.ref('/messages/{messageId}').onCreate((snapshot, context) => {
const fromId = snapshot.val().fromId;
const toId = snapshot.val().toId;
console.log('LOGGER --- fromId is ' + fromId);
console.log('LOGGER --- toId is ' + toId)
var message = snapshot.val();
return admin.database().ref('/users/' + toId).once('value', snapshot => {
var userWhoRecieved = snapshot.val();
return admin.database().ref('/users/' + fromId).once('value', snapshot => {
var userThatSent = snapshot.val();
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
admin.messaging().sendToDevice(userWhoRecieved.fcmToken, payload)
.then(function(response) {
console.log(response.results[0].error);
console.log('Succesfully sent message:', response);
})
.catch(function(error) {
console.log(response.results[0].error);
console.log('Error sending message', error);
});
})
})
})
以下是我尝试获取通知以发送默认声音和振动的方式。
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
当我使用上面的代码发送消息时,我在我的 firebase 函数日志中看到以下错误
我尝试了其他一些方法,但这些方法甚至不会通过终端推送。实现此方法的正确方法是什么?
the issue with sound inside notification object
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
sound: 'default'
},
};
上面的代码行是我得到下面错误的方式。
/Users/wilcox.323/Downloads/OddJobs-master/functions/index.js:219
sound: 'default'
^^^^^
SyntaxError: Unexpected identifier
at wrapSafe (internal/modules/cjs/loader.js:1116:16)
at Module._compile (internal/modules/cjs/loader.js:1164:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
at Module.require (internal/modules/cjs/loader.js:1089:19)
at require (internal/modules/cjs/helpers.js:73:18)
at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:15:15
at Object.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:53:3)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
ODEE236182:OddJobs-master wilcox.323$
【问题讨论】:
标签: ios node.js swift notifications google-cloud-functions