Relm
Relm

Reputation: 8290

How to get Google Cloud Messaging response into a Service worker's event for notifications in Chrome

The message is being received and notification does pop up when I use an example text for notification.

I've set up an account with Google for push notifications on Chrome, but the response appears to be empty.

THE SERVICE WORKER

I have this on the service worker, but its empty.

self.addEventListener('push', function(event) {
  //console.log('Received a push message', event);
  console.log(event.data);
});

Then there's one other thing I've tried with Fetch, using the localhost url.

var url = 'http://localhost/notification/index.php?type=fg';
self.addEventListener('push', function(event) {
  event.waitUntil(fetch(url).then(function(response) {
          console.log(response);
            return response.json();
      }).then(function(data) {
          console.log(data);
          //'data' does't have the json from the url
      })
    )
});

Upvotes: 2

Views: 344

Answers (1)

Marco Castelluccio
Marco Castelluccio

Reputation: 10802

Chrome doesn't support push payloads yet, so the first snippet will only work in Firefox (https://serviceworke.rs/push-payload.html).

Your second approach, instead, should work in any browser (https://serviceworke.rs/push-get-payload.html).

Upvotes: 2

Related Questions