本文介绍了向每个人发送通知,但触发它的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase消息向我的iPhone应用的用户发送通知。我的数据库是这样构造的:

   - 用户
- user1
- user2
- group
- group1
- 会员
- user1
- user2

当用户加入某个组时,他们会订阅对应于该组。我有一个云端函数,用于侦听该群组中的写入,并在发生写入时向群组主题发送通知:

  ();事件=> {
常量pet_Id = event.params.petId;
$ b常量有效载荷= {
'通知':{
'title':`$ {toTitleCase(name)}刚记录一个事件`,
'body': $ {toTitleCase(petName)}`,
'sound':'default',
}
};

admin'$ {events [eventType] .messaging()。sendToTopic(pet_Id,payload);
});

然而,这导致每个人都得到一个通知,包括做这个触发通知的写的人。我只希望组中的其他人显示通知,因为触发用户不需要看到一个。我尝试追加发送用户的uid作为通知的额外数据,并且只在接收用户的uid与通知数据的uid不匹配时才显示通知。这是在应用程序处于前台而不是在后台的情况下起作用,所以如果用户写入,然后在收到通知之前关闭应用程序,那么当他收到通知时,会显示给他,这是我试图避免的。

如何确保只有其他组员可以收到通知?是消息主题不是好的这个?

解决方案

我想这个解决方案是:




  • 每个来自 group1 的iOS客户端都将订阅主题 /topics/group1.selfUserId
  • 服务器已经拥有属于group1的所有用户的列表

  • iOS客户端请求服务器将通知发送到 group1.members 旁边 selfUserId (发出http发送请求)
  • 所有 group1.members selfUserId 旁边并发送通知给所有 /topics/group1.memberX


I'm using Firebase Messaging to send out notifications to users of my iPhone app. My database is structured like this:

- Users
    - user1
    - user2
- Groups
    - group1
        - members
            - user1
            - user2

When a user joins a group they get subscribed to a topic corresponding to that group. I have a cloud function that listens for writes in that group, and sends a notification to the groups topic when a write happens:

exports.sendNotifs = functions.database
    .ref('pets/{petId}/events/{eventId}').onWrite(event => {
        const pet_Id = event.params.petId;

        const payload = {
              'notification': {
                'title': `${toTitleCase(name)} just logged an event`,
                'body': `${events[eventType]} for ${toTitleCase(petName)}`,
                'sound': 'default',
              }
            };

        admin.messaging().sendToTopic(pet_Id, payload);
    });

However, this results in everybody getting a notification including the person who did the write that triggered the notification. I only want other people in the group to display a notification since the triggering user doesn't need to see one. I tried appending the sending user's uid as extra data of the notification and only displaying the notification if the recieving user's uid doesn't match the notification data's uid. This works when the application is in the foreground but not if its in the background, so if the user writes then closes the application before he receives the notification it'll display for him when he receives it, something I'm trying to avoid.

How can I make sure only other members of a group get a notification? Are messaging topics not good for this?

解决方案

i guess that the solution is:

  • each iOS client from group1 will subscribe to topic /topics/group1.selfUserId
  • the server already have the list with all users that are part of group1
  • the iOS client ask server to send notification to group1.members beside selfUserId (make a http post request)
  • server send gets all group1.members beside selfUserId and send notification to all /topics/group1.memberX

这篇关于向每个人发送通知,但触发它的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:12