im尝试从函数发送消息,但始终收到“提供给sendToDevice()的注册令牌必须是非空字符串或非空数组” 但是这是我的令牌存储的位置:
路径
-UID
+++-令牌
这里是我的代码
exports.mess = functions.database.ref('/path/{uid}')
.onWrite(event => {
var ref = admin.database().ref(`token`);
return ref.once("value", function(snapshot){
const payload = {
notification: {
title: 'You have been invited to a trip.',
body: 'Tap here to check it out!'
}
};
admin.messaging().sendToDevice(snapshot.val(), payload)
},function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
});
答案 0 :(得分:0)
我认为您正在尝试使用触发Cloud Function的用户的token
属性值。在这种情况下,您不需要附加单独的侦听器,因为该值在context
参数中已经可用。
exports.mess = functions.database.ref('/path/{uid}')
.onWrite(event => {
let token = event.after.child('token').val()
const payload = {
notification: {
title: 'You have been invited to a trip.',
body: 'Tap here to check it out!'
}
};
return admin.messaging().sendToDevice(snapshot.val(), payload)
});