带有声音和振动的推送通知
Push notifications with sound and vibration
我正在尝试在用户收到消息时向他们发送推送通知,通知成功并显示横幅,但它是静音且不振动的。
我使用 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$
是否振动取决于用户设置。您的应用程序无法取代用户在其设备设置中设置的值。
你检查过你的设备设置了吗?
另外,请看这个问题:
如您所见,用户通过以正确格式发送文件名使一切正常。是否有可能您在有效载荷中指定的声音不正确?
错误如下:
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
声音应该在通知对象内。
参见:https://firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload
您可能会收到错误消息,因为您将声音键放在了通知对象之外。如果你想让它工作,你的负载应该是文档中描述的格式。
编辑:
在您更新的代码中,您忽略了用逗号分隔声音键值对。将您的代码更新为:
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text,
sound: 'default'
},
};
我正在尝试在用户收到消息时向他们发送推送通知,通知成功并显示横幅,但它是静音且不振动的。
我使用 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$
是否振动取决于用户设置。您的应用程序无法取代用户在其设备设置中设置的值。
你检查过你的设备设置了吗?
另外,请看这个问题:
如您所见,用户通过以正确格式发送文件名使一切正常。是否有可能您在有效载荷中指定的声音不正确?
错误如下:
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text
},
sound: 'default'
};
声音应该在通知对象内。
参见:https://firebase.google.com/docs/reference/admin/node/admin.messaging.NotificationMessagePayload
您可能会收到错误消息,因为您将声音键放在了通知对象之外。如果你想让它工作,你的负载应该是文档中描述的格式。
编辑:
在您更新的代码中,您忽略了用逗号分隔声音键值对。将您的代码更新为:
var payload = {
notification: {
title: userThatSent.name + ' sent you a message',
body: message.text,
sound: 'default'
},
};