Cloud Functions无法从实时数据库获取令牌

时间:2019-05-05 20:16:22

标签: javascript firebase firebase-realtime-database google-cloud-functions

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);
        });

       });

1 个答案:

答案 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)
});