javascript - Receive notifications 24/7 via service-worker.js in Chrome -
i've been doing push notifications, when register page test via curl commands, received notification!
however after while (maybe 1-2 minutes), when close tab push notifications scope has been registered, test notifications again, cant receive notifications. happens more in google chrome in mobiles.
the workaround did in here need go page of scoped page first, when test notification again, works. though cant have because need clients receive notifications without being on site time.
here push event in service worker
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 }); }); }) ); });
how make service workers 24/7 though not in page of scope in registration of sw?
do need use eventlisteners 'install', 'activate' , 'fetch'?
service workers appear persist don't - new instance spins in response events. case when restarting on mobile handle push event, there suggestions busy service workers should allow multiple instances in parallel.
your subscriptionid
global parameter, may undefined
time push
event fires.
to fix need retain subscriptionid
in kind of storage (mayber indexeddb).
i think in order service worker persist needs have install
event , event needs succeed.
Comments
Post a Comment