【问题标题】:Cloud Functions cant not get token from realtime databaseCloud Functions 无法从实时数据库中获取令牌
【发布时间】:2019-05-05 20:16:21
【问题描述】:

我试图从函数发送消息,但它总是得到“提供给 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);
        });

       });

【问题讨论】:

  • snapshot.val() 的确切值是多少?尝试记录它。错误消息表明这不是您的想法。看起来您正在查询数据库中的错误位置。您的问题说您的令牌存储在 UID 下,但您的查询根本不考虑 UID。
  • snapshot.val() 的日志如您所说返回 null,但 admin.database().ref(token) 不是从顶部的 /path/{uid} 收集的?
  • 不,它没有达到您的预期。

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


【解决方案1】:

我认为您正在尝试使用触发云功能的用户的 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)
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多