Cloud Functions无法从实时数据库获取令牌 [英] Cloud Functions cant not get token from realtime database

查看:34
本文介绍了Cloud Functions无法从实时数据库获取令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im试图从函数发送消息,但始终收到提供给sendToDevice()的注册令牌必须是非空字符串或非空数组"但是这是我的令牌存储的位置:

im trying to send message from function but its always got "Registration token(s) provided to sendToDevice() must be a non-empty string or a non-empty array" but here is where my token stored:

  • 路径

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

       });

推荐答案

我认为您正在尝试使用触发Cloud Function的用户的 token 属性的值.在这种情况下,您不需要附加单独的侦听器,因为该值已经在 context 参数内.

I think you're trying to use the value of the token property of the user who triggered the Cloud Function. In that case you don't need to attach a separate listener, as the value is already available inside the context parameter.

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

这篇关于Cloud Functions无法从实时数据库获取令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆