javascript - How to persist data in a Service Worker -


as of now, chrome not support passing additional data push notifications received gcm. have execute fetch service worker whenever receive push notification. far, good.

but: need include request parameter in http request executed in fetch. how tell service worker parameter?

what i've tried far

using postmessage tell service worker request parameter:

var serviceworkerdata = {};  self.addeventlistener('message', function (evt)  {     console.log('service worker received', evt.data);      serviceworkerdata = evt.data.mydata; });  self.addeventlistener('push', function(event) {     event.waituntil     (         fetch("http://my.url", {             method: 'post',             body: 'mydata=' + serviceworkerdata         }).then(function(response)         {             //...         })     ); }); 

why not working

but not persistent, i.e. after close browser , open again, serviceworkerdata lost. localstorage not available within service workers, how persistence here?

you can use indexeddb.

https://github.com/mozilla/localforage if want simpler interface indexeddb.

you can import localforage lib in service worker using importscripts, see example: https://github.com/marco-c/mercurius/blob/master/static/sw-push.js.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -