繁体   English   中英

使用 Firebase 云功能向特定用户发送通知

[英]Sending Notification With Firebase Cloud Functions to Specific users

我在我的 firebase 群聊应用程序中使用 firebase 云 function,设置已经完成,但问题是当有人在任何组中发送消息时,所有用户都会收到通知,包括非成员的消息。

我只想向特定组的用户发送通知,下面是我的 firebase 云 function 的代码 -

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

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

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

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

    const payload = {
        notification: {
            title: text,
            body: author,
            icon: ''
        }
    };

    return admin.messaging()
                .sendToTopic('my-groupchat', payload);
 });
});

如果有人可以就此提出建议,这将非常有帮助。

根据我们关于评论的对话,我认为问题在于您正在使用包含所有用户的主题,即my-groupchat主题。 解决方案是使用属于该子主题的用户创建一个新主题。

至于如何创建此类主题,本文档中有几个示例,您可以在其中看到您可以在服务器端或客户端进行。 在您的情况下,您可以按照服务器端的示例进行操作,因为您需要批量添加它们,但是随着新用户的添加,实现客户端方法可能会很有趣。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM