Skip to content

Commit 43b5ee5

Browse files
committed
feat: add conditional push notifications for background chat messages and group invites with focus behavior
1 parent 3cd2266 commit 43b5ee5

1 file changed

Lines changed: 47 additions & 2 deletions

File tree

client/src/Context/GroupMusicContext.jsx

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,20 +271,38 @@ export function GroupMusicProvider({ children }) {
271271
}
272272
}
273273

274+
// Notify only when user is not in this tab or is focused in another app
275+
const isTabInactive =
276+
typeof document !== 'undefined' &&
277+
(document.hidden || (typeof document.hasFocus === 'function' && !document.hasFocus()));
278+
274279
if (
280+
isTabInactive &&
275281
message.type !== 'activity' &&
276282
message.senderId !== user?.userid &&
277283
typeof window !== 'undefined' &&
278284
'Notification' in window &&
279285
Notification.permission === 'granted'
280286
) {
281287
try {
282-
new Notification(message.userName || 'SyncVibe Chat', {
283-
body: message.message,
288+
const bodyText =
289+
message.messageType === 'gif'
290+
? 'Sent a GIF / Media'
291+
: message.messageType === 'sound'
292+
? `Sent sound effect: ${message.soundName || 'Sound Effect'}`
293+
: message.message || 'Sent a message';
294+
295+
const notification = new Notification(message.userName || 'SyncVibe Group Chat', {
296+
body: bodyText,
284297
icon: message.profilePic || '/favicon.ico',
285298
tag: 'syncvibe-group-chat',
286299
renotify: true,
287300
});
301+
302+
notification.onclick = () => {
303+
window.focus();
304+
notification.close();
305+
};
288306
} catch (err) {
289307
console.error('Error displaying notification:', err);
290308
}
@@ -314,6 +332,33 @@ export function GroupMusicProvider({ children }) {
314332
socket.on('group-invite-received', (inviteData) => {
315333
if (ss.getState().currentGroup) return;
316334
inv.setState({ pendingInvite: inviteData });
335+
336+
const isTabInactive =
337+
typeof document !== 'undefined' &&
338+
(document.hidden || (typeof document.hasFocus === 'function' && !document.hasFocus()));
339+
340+
if (
341+
isTabInactive &&
342+
typeof window !== 'undefined' &&
343+
'Notification' in window &&
344+
Notification.permission === 'granted'
345+
) {
346+
try {
347+
const notification = new Notification('SyncVibe Group Invite', {
348+
body: `${inviteData.inviterName || 'Someone'} invited you to join a music group`,
349+
icon: inviteData.inviterProfilePic || '/favicon.ico',
350+
tag: 'syncvibe-group-invite',
351+
renotify: true,
352+
});
353+
354+
notification.onclick = () => {
355+
window.focus();
356+
notification.close();
357+
};
358+
} catch (err) {
359+
console.error('Error displaying invite notification:', err);
360+
}
361+
}
317362
});
318363

319364
socket.on('invite-sent', () => toast.success('Invite sent!'));

0 commit comments

Comments
 (0)