Firebase的云端功能 - 订阅用户到主题

问题描述:

嗨,我想在Node.js环境中使用云端功能为Firebase订阅特定主题的用户,我已跟随此视频https://www.youtube.com/watch?v=GNR9El3XWYo,其中Firebase专家展示了如何订阅用户时,在火力地堡节点写而成,here's我的完整代码: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

问题和错误已更新! – Andrea

在这种情况下,它看起来像你传递的registration_tokens键的单一值。按照API documentation这应该是一个数组。

因此改变registration_tokens: snapshot.val()registration_tokens: [snapshot.val()]应该把该请求的格式API期望