使用Firebase功能将通知推送到组

时间:2017-12-18 01:38:35

标签: ios node.js firebase firebase-realtime-database firebase-cloud-messaging

我正在开发一个使用firebase进行消息传递的应用。我希望在组中有新消息时使用函数发送推送通知。不幸的是,我对node.js没有经验,因为我没有CS背景,只有几个月的编码经验。

我试图将其与我能找到的教程拼凑在一起,但我甚至不确定我是否朝着正确的方向前进。

这适用于iOS应用程序,如果这有任何区别

这是我的火力架结构

enter image description here

这是我到目前为止所拥有的

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPush = functions.database.onWrite(event => {
let projectStateChanged = false;
let projectCreated = false;
let projectData = event.data.val();

if (!event.data.previous.exists()) {
    projectCreated = true;
}
if (!projectCreated && event.data.changed()) {
    projectStateChanged = true;
}
var valueObject = event.data.val();

return loadUsers().then(users => {
    let tokens = [];
    for (let user of users) {
        tokens.push(user.pushToken);
    }
    let payload = {
        notification: {
            title: valueObject.RealName,
            body: valueObject.MessageBody,
            sound: 'default',
        }
    };
    return admin.messaging().sendToDevice(tokens, payload);
});
 });
function loadUsers() {
let dbRef = event.data.ref.parent.parent.ref('/GroupMembers/');
let defer = new Promise((resolve, reject) => {
    dbRef.once('value', (snap) => {
        let data = snap.val();
        let users = [];
        for (var property in data) {
            users.push(data[property]);
        }
        resolve(users);
    }, (err) => {
        reject(err);
    });
});
return defer;

}

1 个答案:

答案 0 :(得分:1)

这是最终的工作代码

let functions = require('firebase-functions');
let admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
// console.log(functions.database.ref('/{pushId}/GroupMembers'));
var pushID = ''
exports.sendPush = functions.database.ref('/{pushId}/messages/{messageID}').onWrite(event => { //THIS IS NOW CORRECT!!

    let projectData = event.data.val();  // this is the actual message data
    // console.log(event.data.val());

    // console.log(event.params);//pushId and messageID
    pushID = event.params.pushId;
    // console.log(pushID);

    return loadUsers().then(users => {

        let tokens = [];

        for (let user of users) {
            if (user.pushToken != projectData.PushToken){
                tokens.push(user.pushToken);
            }

        }
        let payload = {
            notification: {
                title: projectData.RealName,
                body: projectData.MessageBody,
                sound: 'SnowSocialNotificationTone.wav',
            }
        };
        return admin.messaging().sendToDevice(tokens, payload);
    });
});
function loadUsers() {
    // console.log('started loadUsers()');

    let dbRef = admin.database().ref(`/${pushID}/GroupMembers`);

    let defer = new Promise((resolve, reject) => {
        dbRef.once('value', (snap) => {
            let data = snap.val();
            console.log(snap.val());
            let users = [];

            for (var property in data) {
                users.push(data[property]);
            }
            resolve(users);
        }, (err) => {
            reject(err);
        });
    });
    return defer;
}