Jump to content

Browser Notifications for Messages/Notifications


Talus
 Share

Recommended Posts

It would be nice if we could optionally get browser notifications when we receive a new message or notification. Some notifications could get spammy, so we'd need the ability to filter certain kinds of notifications. For example: show all baseball notifications, but hide trade notifications.

  • Upvote 2
Link to comment
Share on other sites

I'm using most recent Firefox, and since update, I did not get any notification at all.

Since you said that you didn't know how to use it and it's something that I could love to have, below is a little snippet of code in javascript that I used in another page and works there:

var notify = function(text, url = '') {
    // if browser does not have notification object, just show a simple alert
    if (!("Notification" in window)) {
        alert('Notification: ' + text);
        return;
    }

    // if browser already gave permission to show notifications, generate the notification!
    if (Notification.permission === "granted") {
        generateNotification(text, url);
        return;
    }
    
    /* when browser didn't gave permission yet (like the first time), request the permission, once is granted, show some message */
    if (Notification.permission !== 'denied') {
        Notification.requestPermission(function (permission) {
        if (permission === "granted") {
            /* below is a message that person accepted the notifications */
            generateNotification('Notifications granted.', '');
            /* below is the first notification message that player received */
            generateNotification(text, url);
            return;
        }
    });
  }
};

var generateNotification = function(text, url) {
    var options = {
        body: text,
        /* inside icon key, you can put some image for showing inside notification */
        icon: window.location.protocol + '//' + window.location.host + '/img/someimage.png'
    };
    
    var notification = new Notification('Politics and War', options);
    
    /* if you use generateNotification("some notification", "http://politicsandwar.com/someurl"),
      if someone click inside the notification, it will redirect to this url. */
    if (url) {
        notification.onclick = function(e) {
            e.preventDefault();
            /* you can use _blank or _self for opening a new page or use current page */
            window.open(url, '_blank');
        };
    }
};

generateNotification("first notification", "https://politicsandwar.com/");
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and the Guidelines of the game and community.