我一直在做推送通知, 当我注册页面,然后通过curl命令测试,我收到通知!在Chrome中通过service-worker.js 24小时接收通知
但过了一段时间(可能是1-2分钟),当我关闭推送通知范围已注册的选项卡时,然后再次测试通知,我无法收到通知。谷歌浏览器在手机中通常会发生这种情况。
我在这里做的解决方法是,我需要首先进入范围页面的页面,然后再次测试通知时,它现在可以工作。虽然我不能这样做,因为我需要客户收到通知,而不是一直在网站上。
这里有服务人员我推送事件
self.addEventListener('push', function(event) {
var SOME_API_ENDPOINT = "https://somewhre.com/push_notification_api/service_worker_endpoint";
event.waitUntil(
fetch(SOME_API_ENDPOINT, {
method: 'post',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
},
body: 'subscription_id=' + subscriptionId
}).then(function(response) {
return response.json().then(function(data) {
var title = data.notification.title;
var message = data.notification.message;
var icon = base + "assets/images/logo_notification.png";
var notificationTag = data.notification.url;
// var notificationTag = 'https://google.com'; //data.notification.tag;
return self.registration.showNotification(title, {
body: message,
icon: icon,
tag: notificationTag
});
});
})
);
});
如何让我的服务人员每天24小时,即使我在SW登记范围的页面不是?
我需要使用eventListeners'install','activate'和'fetch'吗?