加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angular5 – 单击@ angular / service-worker推送通知的操作部分

发布时间:2020-12-17 17:43:32 所属栏目:安全 来源:网络整理
导读:我一直在尝试将用户重定向到来自后端( PHP)的Webpush的“动作”部分. return (new WebPushMessage) -title('Title') -icon('icon.png') -body('Body Msg') -action('Open Notification','open_notification') -data(['id' = $notification-id,'url'='http://
我一直在尝试将用户重定向到来自后端( PHP)的Webpush的“动作”部分.

return (new WebPushMessage)
        ->title('Title')
        ->icon('icon.png')
        ->body('Body Msg')
        ->action('Open Notification','open_notification')
        ->data(['id' => $notification->id,'url'=>'http://somewhere']);

默认服务工作者使用:

self.addEventListener('notificationclick',function(event) {
    console.log('On notification click: ',event.notification.tag);
    event.notification.close();

    // This looks to see if the current is already open and
    // focuses if it is
    event.waitUntil(clients.matchAll({
       type: "window"
    }).then(function(clientList) {
       for (var i = 0; i < clientList.length; i++) {
          var client = clientList[i];
              if (client.url == '/' && 'focus' in client)
                 return client.focus();
       }
       if (clients.openWindow)
          return clients.openWindow('/');
    }));
});

从https://developer.mozilla.org/en-US/docs/Web/Events/notificationclick起

Angular 5使用

import {SwPush,SwUpdate} from '@angular/service-worker';

那么我的问题是如何使用@ angular / service-worker在前端(Angular 5)中解释这一点

解决方法

得到了回复 u-ryo在Github上

有一个解决方法.
将以下代码添加到该行周围的ngsw-worker.js中
this.scope.addEventListener(‘push’,(event)=> this.onPush(event));(第1775行).

this.scope.addEventListener('notificationclick',(event) => {
    console.log('[Service Worker] Notification click Received. event',event);
    event.notification.close();
    if (clients.openWindow && event.notification.data.url) {
      event.waitUntil(clients.openWindow(event.notification.data.url));
    }
  });

然后,您可以在“notification.data.url”中指定URL.

https://github.com/angular/angular/issues/20956#issuecomment-374133852

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读