在聊天应用程序中反应本机通知

时间:2018-07-23 09:18:51

标签: firebase react-native push-notification firebase-cloud-messaging firebase-notifications

我正在开发一个聊天应用程序,目前我有6个不同的聊天频道,每个频道都有不同的Firebase数据库。每个通道都有Firebase规则,这些规则定义用户是否可以在此通道中读写。我想在发布新消息时向用户发送通知,但仅发送属于指定频道的用户。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const _ = require('lodash');

admin.initializeApp(functions.config().firebase);

exports.sendNewMessageNotification = functions.database.ref('/GeneralMessage').onWrite(event => {
    const getValuePromise = admin.database()
                                 .ref('GeneralMessage')
                                 .orderByKey()
                                 .limitToLast(1)
                                 .once('value');

    return getValuePromise.then(snapshot => {
        console.log(_.values(snapshot.val())[0]);
        const { text } = _.values(snapshot.val())[0];

        const payload = {
            notification: {
                title: 'New msg',
                body: text,
            }
        };

        return admin.messaging()
                    .sendToTopic('GeneralMessage', payload);
    });
});

这是我目前在/functions/index.js中使用firebase cloud函数的代码。当我在应用程序中发送消息时,我的输出在firebase中:Firebase cloud functions,但通知在应用程序中不起作用。

0 个答案:

没有答案