ios - 通过声音和振动推送通知
问题描述
我正在尝试在用户收到消息时向他们发送推送通知,通知成功并显示横幅,但它是静音且不振动的。
我使用 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'
},
};
推荐阅读
- javascript - 如何在 Google App Script 中比较 2 个数组中的元素(如果不存在则添加)?
- python - 是否保证链式 celery 任务在同一个节点工作程序上运行?
- javascript - 如何从 url 获取 json 数据并将其保存到 const 变量 [TypeScript]
- javascript - JS输入值分配给变量 - 未定义
- richtext - WEEX - iOS 上的富文本组件
- python - Keras ctc_decode 形状必须为 1 级,但为 2 级
- javascript - 一个 svg 元素在另一个元素后面移动的触发事件?
- marklogic - 从文件系统加载 MarkLogic 中的文档时权限被拒绝错误
- javascript - 使用 jQuery / JavaScript 使元素向右浮动
- cassandra - 启用一次 Cassandra 身份验证和授权检查并永久缓存