javascriptangularjsbrowsernotificationsweb-notifications

Angularjs notification showing twice if on 2 browser tabs


I am using angular-web-notification . My problem is that if I open 2 browser tabs and then the messages are received, I get to see two instances of notifications shown.

Below is the reference code.

    webNotification.showNotification(title, {
        body: body,
        onClick: function onNotificationClicked() {
            console.log('Notification clicked.');
        },
        icon: 'icon.png'
    }, function onShow(error, hide) {
        if (error) {
            console.log('Unable to show notification: ' + error.message);
        } else {
            console.log('Notification Shown.');
        }
    });

Do you have any ideas?


Solution

  • To prevent any duplicate notification use Tag

    Notification.tag The ID of the notification https://developer.mozilla.org/en-US/docs/Web/API/notification

     webNotification.showNotification(title, {
            body: body,
            tag:"some custom unique tag per notification"
            onClick: function onNotificationClicked() {
                console.log('Notification clicked.');
            },
            icon: 'icon.png'
        }, function onShow(error, hide) {
            if (error) {
                console.log('Unable to show notification: ' + error.message);
            } else {
                console.log('Notification Shown.');
            }
        });