【问题标题】:Firebase Cloud Functions: Cannot pass the token retrieved from Realtime DatabaseFirebase Cloud Functions:无法传递从实时数据库检索到的令牌
【发布时间】:2019-03-18 11:08:50
【问题描述】:

我在使用云函数的 admin.database() 检索保存在实时数据库中的令牌时遇到问题。只有一个令牌可以从孩子那里读取。

Firebase 数据库结构

这是我在 Index.js 中的代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();

exports.sendNotification = functions.database
.ref('/Logs/{LogsID}')
.onWrite( (change, context) => {
    const notificationSnapshot = change.after.val();
    const status = notificationSnapshot.Status;
    const time = notificationSnapshot.Time;
    const payload = {
        notification: {
            title : status,
            body : time
        }
    }
    console.info(notificationSnapshot);
    const pushToken = admin.database().ref('/Tokens').once('child_added').then( (data) => {
        const tokenSnapshot = data.val();
        const finaltoken = tokenSnapshot.token;
        console.info(finaltoken);
    })

// Need help down here.

    admin.messaging().sendToDevice(finaltoken, payload)
    .then( () => {
        console.log('Notification sent');
    })
    .catch( () =>{
        console.log('Notification failed');
    })
    return null;
});

finalToken 按预期在日志中显示正确的令牌。 Log Showing the token

但是当我将相同的令牌传递给 admin.messaging() 时出现错误。控制台正在记录“已发送通知”但未收到通知。

ReferenceError: finaltoken 未定义 在exports.sendNotification.functions.database.ref.onWrite (/user_code/index.js:43:36) 在 cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23) 在 cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20) 在 /var/tmp/worker/worker.js:827:24 在 process._tickDomainCallback (internal/process/next_tick.js:135:7)

当我直接传递令牌时它可以工作,

var finalToken = 'ephrj1........kndji'

所以 admin.messaging() 工作,只传递令牌是行不通的。

我是 Cloud Functions 和 javascript 的新手,非常感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    正在回调/异步函数中检索最终令牌。

    这意味着当您将其添加到 .sendToDevice() 时,令牌未定义,因为异步函数尚未从数据库中检索令牌......。

    const pushToken = admin.database().ref('/Tokens').once('child_added').then( (data) => {
            const tokenSnapshot = data.val();
            const finaltoken = tokenSnapshot.token;
            console.info(finaltoken);
    
            admin.messaging().sendToDevice(finaltoken, payload)
            .then( () => {
              console.log('Notification sent');
            })
            .catch( () =>{
              console.log('Notification failed');
            })
    
           // I moved admin.messaging above this bracket
      })
    
    // It used to be here
    
    return null;
    

    尝试将admin.messaging 代码放在(data) => {} 的代码块中

    通过这样做,我们确保每当我们调用 sendToDevice() 时,都会定义令牌。

    【讨论】:

    • 完全没问题:)
    猜你喜欢
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2020-09-23
    • 2019-11-28
    • 1970-01-01
    • 2023-04-03
    • 2020-11-30
    • 2019-11-26
    相关资源
    最近更新 更多