【发布时间】: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