0

Google Chrome latest(v55.0.2883.87) There are various event in that (sw.js)file. Everytime a file got requested fetch event occur. How and when other events occur(sync, push)(web notification api). I want to debug it. Is there any doc available?

Update: server-key-from-firebase-console

subscription-key-after-subscribing-web-notification

Found how push notification fired--

        String url = "https://fcm.googleapis.com/fcm/send";
        URL obj = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

        //add reuqest header
        con.setRequestMethod("POST");
        con.setRequestProperty("Authorization", "key=<server-key-from-firebase-console>");
        con.setRequestProperty("Content-Type", "application/json");

        String urlParameters = "{\"to\":\"<subscription-key-after-subscribing-web-notification>\"}";

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(urlParameters);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //print result
        System.out.println(response.toString());

Update 2: Ok now i found how to fire a sync event in service worker file either from google chrome debugger tool or from javascript. Below is the code, what i have found on google's blog post.

// Register your service worker:
navigator.serviceWorker.register('/sw.js');

// Then later, request a one-off sync:
navigator.serviceWorker.ready.then(function(swRegistration) {
  return swRegistration.sync.register('myFirstSync');
});

Then listen for the event in /sw.js:

self.addEventListener('sync', function(event) {
  if (event.tag == 'myFirstSync') {
    event.waitUntil(doSomeStuff());
  }
});
karman
  • 346
  • 3
  • 20
  • You can listen also for notifications: `self.addEventListener('push', (event) => { });`. And the notification is raise when you send a notification via Firebase Notifications for example. – Hosar Jan 11 '17 at 14:09

1 Answers1

0

A good resource for knowing more about ServiceWorker is MDN (Mozilla Developer Network).

Here is the entry point to the documentation related to ServiceWorker:

https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API

There you have links and explanations for the install, fetch, sync and push events. And the new ones that will appear like background sync will be documented there too.

Happy reading.

  • Yes, i know about backgroud sync, but i didn't know about how to trigger events. I have a separate question on that too, which i have updated. http://stackoverflow.com/questions/41394299/service-worker-recurring – karman Jan 13 '17 at 07:30