Ask coding questions

← Back to all posts
How do I send a desktop notification when a user sends a message in my chatroom?
h
UnluckyFroggy (917)

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

Comments
hotnewtop
GatewayDuckYT (48)
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
UnluckyFroggy (917)

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

GatewayDuckYT (48)

@UnluckyFroggy done

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

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