javascriptnotificationsweb-notifications

Hide JS Notification Object By Tag Name


I'm using the notification API for my project to show browser notifications where each notification has a unique tag (ID), but I can't seem to find a way to close or hide the notification by the tag name, without calling the close function on the object, since it might be closed with other pages than where it was originated. Is this sort of thing possible?


Solution

  • You could save the notifications in localStorage and then retrieve it and close.

    e.g.

    // on create
    var n = new Notification('Notification Title', {
            tag: _this.attr('data-notification-id')
        });
    window.localStorage.setItem('data-notification-id', n);
    

    and

    // then later
    var n = window.localStorage.getItem('data-notification-id');
    n.close();