Ashwini Prajapati
Ashwini Prajapati

Reputation: 11

For Web, Getting multiple firebase notification if same application open in multiple tabs

I am using @angular/fire for firebase push notification with angular 7. I am getting same notification as much time as my application is open in multiple tabs.

receiveMessage() {
    this.angularFireMessaging.messages.subscribe(
      (payload) => {
        console.log("new message received. ", payload);
        // this.currentMessage.next(payload);
        console.log(thisref._router.url);
        if(thisr[enter image description here][1]ef._router.url == '/playGame' && payload.data.gameId == window.localStorage.getItem(environment.gameId)) {
          return
        } else{
          var notificationTitle = payload.notification.title;
          var notificationOptions = {
            body : payload.notification.body,
            icon : payload.notification.icon,
            // click_action : payload.notification.click_action
          };
          console.log(notificationOptions);
          var notification = new Notification(notificationTitle,notificationOptions);
          notification.onclick = function(event) {
              // event.preventDefault(); // prevent the browser from focusing the Notification's tab
              window.open(payload.notification.click_action , '_self');
              // notification.close();
          }
        }
      })
}

Upvotes: 0

Views: 1286

Answers (1)

Ashwini Prajapati
Ashwini Prajapati

Reputation: 11

I got one notification for each tab in browser in foreground.So I set below code and it worked for me.

if (!document.hidden) {
            var notificationTitle = payload.notification.title;
            var notificationOptions = {
              body : payload.notification.body,
              icon : payload.notification.icon,
              // click_action : payload.notification.click_action
            };
            console.log(notificationOptions);
            var notification = new Notification(notificationTitle,notificationOptions);
            notification.onclick = function(event) {
                // event.preventDefault(); // prevent the browser from focusing the Notification's tab
                window.open(payload.notification.click_action , '_self');
                // notification.close();
            }
          }

Upvotes: 1

Related Questions