【发布时间】:2018-12-11 11:12:05
【问题描述】:
我尝试使用 node js 和 firebase 函数向客户端设备发送特定消息。但是当我尝试执行该函数时,它返回了一条错误消息:
错误。提供给 sendToDevice() 的注册令牌必须是非空字符串或非空数组。
图片如下所示。
我猜它来自我的 JS 代码。所以我也发布了。我实际上要做的是从特定节点检索数据,以便在写入完全不同的节点时使用。所以我会在数据库截图之前发布JS代码。
exports.sendNotification8 = functions.database.ref('/Users/{user_id}/Notifications/')
.onWrite(( change,context) =>{
var user_id = context.params.user_id;
// Grab the current value of what was written to the Realtime Database.
var eventSnapshot = change.after.val();
var device_token = admin.database().ref('/Users/{user_id}/device_token').once('value');
return device_token.then(result => {
var token_id = result.val();
var str = eventSnapshot.from + " : " + eventSnapshot.message;
console.log(eventSnapshot.from);
var payload = {
data: {
name: str,
title: eventSnapshot.from,
click_action: "Chats"
}
};
// Send a message to devices subscribed to the provided topic.
return admin.messaging().sendToDevice(token_id, payload).then(function (response) {
// See the MessagingTopicResponse reference documentation for the
// contents of response.
console.log("Successfully sent message:", response);
return;
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
});
这就是我检索 device_token 节点的方式。来自将最新数据写入其通知节点的用户。请帮忙。我做错了什么?
【问题讨论】:
标签: javascript android firebase firebase-realtime-database firebase-cloud-messaging