Skip to content
Sign upLog in
← Back to Community

How do I send a desktop notification when a user sends a message in my chatroom?

Profile icon
UnluckyFroggy

My Notifications code, so far:

(async () => { Notification.requestPermission(permission => { if (permission === 'granted') { if (notifications == true) { const myNoti = new Notification(`Froggy Chatroom`, { body: `USER_MESSAGE_HERE`, icon: './favicon.ico' }); myNoti.onclick = function (e) { window.focus(); } } } // navigate to a URL when clicked notification.addEventListener('click', () => { window.open('https://froggy-chatroom.unluckyfroggy.repl.co', '_blank'); }); }); // check notification permission let granted = false; if (Notification.permission === 'granted') { granted = true; new Notification(`Froggy Chatroom`, { body: `Welcome!`, icon: './favicon.ico' }); } else if (Notification.permission !== 'denied') { let permission = await Notification.requestPermission(); granted = permission === 'granted' ? true : false; } })();

The variable for the messages are "message".

Also, here's a link to the Testing Chatroom: Chatroom

Voters
Profile icon
BenMoodie
Profile icon
Astrroidd
Profile icon
UnluckyFroggy
Comments
hotnewtop
Profile icon
GatewayDuckYT
var notify = new Notification('Hi there!', { body: 'How are you doing?', icon: 'https://bit.ly/2DYqRrh', });

there this will only work if the user is in your site

this helped.? check the uparrow
Profile icon
UnluckyFroggy

@GatewayDuckYT
no, I need the body to be my message variable.

Profile icon
GatewayDuckYT

@UnluckyFroggy
done

var notify = new Notification('Hi there!', { body: body, icon: 'https://bit.ly/2DYqRrh', });
Profile icon
UnluckyFroggy

Hope you can help, it's a hard question.