【问题标题】:Get data from Firebase Real Time Database from a Cloud Function通过 Cloud Function 从 Firebase 实时数据库获取数据
【发布时间】:2018-09-09 12:14:03
【问题描述】:

我正在使用 firebase 功能在 android 设备上发送数据类型推送通知。我正在使用这个 index.js 脚本。当用户在 firebase 数据库中添加新消息时,我正在从 firebase 数据库中获取用户 ID。

现在我想使用这个用户 ID 来获取用户的 fcmToken。

索引.js

//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


// Listens for new messages added to messages/:userId
exports.pushNotificationData = functions.database.ref('/messages/{userId}').onWrite( event => {

    console.log('Push notification event triggered');

    //  Grab the current value of what was written to the Realtime Database.
    var valueObject = event.data.val();

    if(valueObject.photoUrl !== null) {
      valueObject.photoUrl= "Sent you a photo!";
    }

  // Create a notification
    const payload = {
        data: {
            title:valueObject.name,
            body: valueObject.text || valueObject.photoUrl,
            sound: "default"
        },
    };

  //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };

 const user_id = event.params.userId;
return admin.messaging().sendToDevice(user_id, payload);


});

它是firebase数据库中用户配置文件的结构,我想从中获取fcmToken。

【问题讨论】:

  • 在你的数据库结构中,userId对应的是什么? profiles节点下的节点(即3、4、5、11、13)或id下的节点(字段)的值3
  • (3, 4, 5, 11, 13) 这些是用户 ID

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


【解决方案1】:

以下应该可以解决问题。您可以通过使用once() 方法查询'profiles/' + user_id 引用来获取令牌的值。由于once() 是异步的并返回一个promise,因此您必须等待promise 解析才能发送消息。

exports.pushNotificationData = functions.database
  .ref('/messages/{userId}')
  .onWrite(event => {
    console.log('Push notification event triggered');

    //  Grab the current value of what was written to the Realtime Database.
    var valueObject = event.data.val();

    if (valueObject.photoUrl !== null) {
      valueObject.photoUrl = 'Sent you a photo!';
    }

    // Create a notification
    const payload = {
        data: {
            title:valueObject.name,
            body: valueObject.text || valueObject.photoUrl,
            sound: "default"
        }
    };

    //Create an options object that contains the time to live for the notification and the priority
    const options = {
      priority: 'high',
      timeToLive: 60 * 60 * 24
    };

    const user_id = event.params.userId;

    return admin
      .database()
      .ref('profiles/' + user_id)
      .once('value')
      .then(snapshot => {
        const token = snapshot.val().fcmToken;
        return admin.messaging().sendToDevice(token, payload, options);
      })
      .catch(error => {
        console.log('Error sending message:', error);
        return false;
      });
  });

此外,请注意您使用的是旧版本的 Cloud Functions (新语法。见https://firebase.google.com/docs/functions/beta-v1-diff

【讨论】:

    猜你喜欢
    • 2022-01-04
    • 2018-08-09
    • 1970-01-01
    • 2019-09-25
    • 2018-05-01
    • 2023-03-06
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    相关资源
    最近更新 更多