Для Интернета: получение нескольких уведомлений Firebase, если одно и то же приложение открыто на нескольких вкладках. - PullRequest
0 голосов
/ 05 февраля 2020

Я использую @ angular / fire для firebase pu sh с уведомлением angular 7. Я получаю одно и то же уведомление столько раз, сколько мое приложение открыто на нескольких вкладках.

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();
          }
        }
      })
}

1 Ответ

0 голосов
/ 10 февраля 2020

Я получил одно уведомление для каждой вкладки в браузере на переднем плане. Так что я установил ниже код, и он работал для меня.

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();
            }
          }
...