Я использую функцию firebase для отправки push-уведомлений типа данных на устройства Android. Я использую этот скрипт index.js. Когда пользователь добавляет новое сообщение в базу данных firebase, я получаю userID из базы данных firebase.
Теперь я хочу использовать этот идентификатор пользователя для получения fcmToken пользователя.
index.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.