我一直在尝试将用户重定向到来自后端(PHP)的Web推送的“操作”部分。
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 5)中使用@angular/service-worker来解释这一点
发布于 2018-04-01 20:58:43
在Github上收到来自u-ryo的回复
有一种解决方法。将以下代码添加到行this.scope.addEventListener('push',(event) => this.onPush(Event));(第1775行)周围的ngsw-worker.js中。
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
发布于 2018-06-17 05:51:10
如果你想要有动作,并且每个动作都有不同的url,这是我的解决方案。
this.scope.addEventListener('notificationclick', (event) => {
event.notification.close();
var payload = event.notification.data;
var url = payload.url;
if (event.action && payload.actions && payload.actions.length) {
var actions = payload.actions.filter(x => x.action == event.action);
if (actions.length && actions[0].url) {
url = actions[0].url;
}
}
if (clients.openWindow && url) {
event.waitUtil(clients.openWindow(url));
}
}); https://stackoverflow.com/questions/48972973
复制相似问题