本文介绍了Firebase Cloud功能:无法传递从实时数据库检索到的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having issues in retrieving a token saved in realtime database using cloud function's admin.database(). There is only one token to read from the child.

Firebase数据库结构

Firebase Database structure

这是我在Index.js中的代码

Here's my code in 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按预期在日志中显示正确的令牌. 显示令牌的日志

finalToken shows the correct token in log as expected. Log Showing the token

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

But I'm getting error while I'm passing the same token to admin.messaging(). Console is logging 'Notification sent' but not receiving a notification.

ReferenceError:未定义finaltoken 在export.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)

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

当我像这样直接传递令牌时,它会起作用

It works when I directly pass the token like,

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

因此admin.messaging()有效,仅传递令牌无效.

so the admin.messaging() works, only passing the token is not working.

我是Cloud Functions和javascript的新手,因此非常感谢您的帮助.

I'm new to Cloud Functions and javascript, so any help is much appreciated.

推荐答案

正在通过回调/异步函数检索最终令牌.

Final token is being retrieved in callback / async function.

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

That means that when you add it to .sendToDevice() the token is undefined because the async function has not retrieved the token from the database... yet.

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()时都定义了令牌.

By doing this we ensure that whenever we call sendToDevice() the token is defined.

这篇关于Firebase Cloud功能:无法传递从实时数据库检索到的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:07