Send notification to everybody but user who trigge

2019-08-01 03:16发布

问题:

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.