Send notification to everybody but user who triggered it?

a 夏天 提交于 2019-12-11 05:57:25

问题


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?


回答1:


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



回答2:


If you use Topics it's not possible to send to everyone except one.

If you are Ok sending to everyone, and then filtering on the client, you will need to use the data messages, and not notification messages, to avoid the problem with background/foreground you described. https://firebase.google.com/docs/cloud-messaging/concept-options




回答3:


As already mentioned, you cannot exclude someone who has been registered to a Topic.

So what you can do is sending a message to a single device. So for example you have a group of ten persons, one person posts a message, you have to send nine single messages to the other nine persons of the group.

What you need to do is to store the registration token of every single user into your database and you have to take into account that registration tokens will change after some time.



来源:https://stackoverflow.com/questions/43176979/send-notification-to-everybody-but-user-who-triggered-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!