服务人员未在Windows 10中使用Chrome接收旧的推送通知单击事件

问题描述 投票:1回答:1

当我们在Windows 10的通知历史记录中有2个以上的推送通知时,则单击最新的通知会在浏览器中打开镶边。但是在Firefox和Edge浏览器中,所有通知都在浏览器中打开。

这是我的服务人员代码。推送事件:

if (!(self.Notification && self.Notification.permission === 'granted')) {
        return;
    }
    var data = {};
    if (event.data) {
        data = event.data.json();
    } else {
        console.log('received empty data for push notification');
    }
    let notificationTitle = data.title;
const notificationOptions = {
        body: data.bodyText,
        icon: data.imageUrl,
        sticky:false,
        notificationCloseEvent:false,
        data: {
          url: data.conversationUrl,
        },
      };
     const message = {notificationTitle, notificationOptions};
      const promise = isClientFocused()
                        .then((clientIsFocused) => {
                            if (clientIsFocused) {
                                console.log('App is in focus no need to show notification');
                                return;
                            } else {
                                return firstWindowClient().then((windowClient) => {
                                    console.log('URL is already in open');
                                    windowClient.postMessage(message);
                                }, () => {
                                    console.log('Url is not in open.');
                                    return self.registration.showNotification(notificationTitle, notificationOptions);
                                });
                            }
                        });
      event.waitUntil(promise);
    });

通知点击事件:

self.addEventListener('notificationclick', (event) => {
       const urlToOpen = new URL(event.notification.data.url, self.location.origin).href;

  const promiseChain = clients.matchAll({
    type: 'window',
    includeUncontrolled: true
  })
  .then((windowClients) => {
    let matchingClient = null;

    for (let i = 0; i < windowClients.length; i++) {
      const windowClient = windowClients[i];
      if (windowClient.url === urlToOpen) {
        matchingClient = windowClient;
        break;
      }
    }

    if (matchingClient) {
      return matchingClient.focus();
    } else {
      return clients.openWindow(urlToOpen);
    }
  });

  event.waitUntil(promiseChain);
    });
service notifications push onclicklistener worker
1个回答
0
投票

您能够解决问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.