I receive data from the server and use it to display notifications to users.
My goal is to send notifications to everyone except the sender, but I'm not sure how to loop through the user data to achieve this.
Code
I have included comments for clarity in the code snippet below
// Notify group
this.socket.fromEvent('message').subscribe(async (message: any) => {
console.log('group message: ', message); // see screenshot below
// Loop through group users and exclude the sender, then send notification to everyone else
// Accessing group users - tested `message.msg.message.group.users`
// Need help with looping through here
// Condition to avoid sending notification to the sender
// Sender's username - tested `message.msg.message.user.username`
// if (message.msg.message.user.username ............??? ) {
const notifs = LocalNotifications.schedule({
notifications: [
{
title: message.msg.message.user.username,
body: message.msg.message.note,
id: Math.floor(Math.random() * 10),
schedule: { at: new Date(Date.now() + 1000 * 2) },
sound: 'beep.wav',
attachments: null,
actionTypeId: 'OPEN_CHAT',
extra: null
}
]
});
console.log('scheduled notifications', notifs);
// }
});
// Notify group
Screenshot
https://i.sstatic.net/wsIO1.png
Any assistance on accomplishing this task would be greatly appreciated. Thank you!
Update
To clarify:
LOGIC
- Each user has a unique
username
- The user object is the sender of the notification
- Group users are members of the group that the notification is sent to (including the sender)
- The purpose of looping through
group users
is to exclude the sender in order to prevent them from receiving the notification Thus, everyone in the group will receive the notification except the sender
Hope this explanation clarifies things a bit.