首页 > 解决方案 > 如何专注于标签,在Angular 8中点击通知?

问题描述

目标是当用户点击浏览器通知时,查看页面是否已经打开并转到它,如果没有,则在新选项卡或窗口中打开它。

使用此代码使用本机服务人员时,这是可行的

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

但是在角度上,他们有自己的服务工作者包装器,它会在运行时生成服务工作者文件。

我现在的角度代码是这样的

  this.swPush.notificationClicks.subscribe(
    ({ action, notification }) => {
      if (notification && notification !== null && notification['data']) {
        const url = notification['data'].url;
        window.open(url, '_blank');
      }
    });

那么首先有没有办法在角度(swPush)中实现这个?

如果不是,我是否必须编辑生成的服务工作者文件,以及如何(最佳实践)?或者我可以导入另一个服务工作者文件吗?

标签: javascriptangularpush-notificationservice-worker

解决方案


推荐阅读