firebasefunction来触发通知

我想创build一个firebase函数,当表中有新的条目时会触发一个通知。 我有包含所有设备令牌的单独的表。 无法弄清楚如何从表中获取每个设备令牌。 这里是我迄今为止写的一些firebase函数的代码片段,但没有运气。

'use strict'; const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); exports.sendNotificationToAllMembers = functions.database.ref('/chatRoom/{messageNode}').onWrite(event => { const messageInfo = event.params.messageNode; // Get the list of device notification tokens. const getDeviceTokensPromise = admin.database().ref(`/deviceTokens`).once('value'); // Get newly added Message detail const getMovieMessageDetail = admin.database().ref(`/chatRoom/${messageInfo}`).once('value'); return Promise.all([getDeviceTokensPromise, getMovieMessageDetail]).then(results => { const deviceTokensList = results[0]; const MessageDetail = results[1]; console.log('device tokens : ',deviceTokensList); console.log('Fetched message detail', MessageDetail); // Check if there are any device tokens. if (!deviceTokensList.hasChildren()) { return console.log('There are no notification tokens to send to.'); } console.log('There are', deviceTokensList.numChildren(), 'tokens to send notifications to.'); // Notification details. const payload = { notification: { title: 'New Movie discussion', body: `${MessageDetail.messageText}`, } }; // Listing all tokens. const tokens = Object.keys(deviceTokensList.val()); // Send notifications to all tokens. return admin.messaging().sendToDevice(tokens, payload).then(response => { // For each message check if there was an error. const tokensToRemove = []; response.results.forEach((result, index) => { const error = result.error; if (error) { console.error('Failure sending notification to', tokens[index], error); // Cleanup the tokens who are not registered anymore. if (error.code === 'messaging/invalid-registration-token' || error.code === 'messaging/registration-token-not-registered') { tokensToRemove.push(deviceTokensList.ref.child(tokens[index]).remove()); } } }); return Promise.all(tokensToRemove); }); }); }); 

这里是我所指的firebase实时数据库的快照,只要在'chatRoom'表中有一个推入的IE,我想发送一个通知给'deviceTokens'表中的每个设备令牌。 Firebase实时数据库快照

感谢提前一吨!