您好我想在Node.js环境中使用Cloud Functions for Firebase为用户订阅特定主题,我已关注此视频https://www.youtube.com/watch?v=GNR9El3XWYo,其中Firebase专家展示了如何订阅用户的时间写入Firebase中的节点,这是我的完整代码:
//For subscriptions
exports.privateEventSubs = functions.database.ref("Events/{eventID}/guest_mod/assistants/{guestEmail}").onWrite((event) => {
let data = event.data;
let eventID = data.ref.parent.parent.parent.key;
console.log("The eventID is: " + eventID);
let guestEmail = data.ref.key;
console.log("The guestEmail is: " + guestEmail);
//Add or remove a subscription
let action = data.exists() ? "batchAdd" : "batchRemove";
console.log("The action is: " + action);
//Get the device token from each user
admin.database().ref("Users/" + guestEmail + "/chat_data/firebaseToken").once("value").then(function (snapshot) {
console.log("The user token is: " + snapshot.val());
//Send messages to users that have subscribed to that event
functions.config().firebase.credential.getAccessToken().then(function (oauthToken) {
console.log("The oauthToken is: " + oauthToken.access_token);
//Lets configure and request
request({
url: "https://iid.googleapis.com/iid/v1:" + action, //URL to hit
method: 'POST',
json: true,
headers: {
Authorization: "Bearer " + oauthToken.access_token,
access_token_auth: true,
},
body: {
to: "/Events/" + eventID,
registration_tokens: snapshot.val()
}
}, function (err, res, body) {
if (err) {
console.log(err);
} else {
console.log(res.statusCode, body);
}
});
});
});
});
我得到的错误就是这个:
400 { error: 'InvalidListOfTokens' }
我做错了什么?请帮忙! :(
答案 0 :(得分:0)
在这种情况下,您似乎正在为registration_tokens
键传递单个值。根据{{3}},这应该是一个数组。
因此,将registration_tokens: snapshot.val()
更改为registration_tokens: [snapshot.val()]
会将请求置于API期望的格式中。