10

I am able to send push notification and in service worker i am making a service call i just to want to send GCM registration id with that service call. How to get registration id or subscription id in service worker

here is my code

self.addEventListener('push', function(event) {
  console.log('Received a push message from local', event);

  var title = 'My title file. Testing on';
  var body = 'New Push Message.';
  var icon = 'refresh_blueicon.png';
  var tag = 'my-push-tag';

  event.waitUntil(
// Here i need to wind GCM Registration id / Subscription id with external service call


  fetch('http://localhost/pushMsg/Push_Notification/msg.php').then(function(response){

     if (response.status !== 200) {
        console.log('Looks like there was a problem. Status Code: ' +
        response.status);
        throw new Error();
      }
       // Examine the text in the response
      return response.json().then(function(data) {

       self.registration.showNotification(data.title, {
          body: data.msg,
          icon: icon,
          tag: tag
        })
  })
  })


  );
});


self.addEventListener('notificationclick', function(event) {
  console.log('On notification click: ', event.notification.tag);
  // Android doesn’t close the notification when you click on it
  // See: http://crbug.com/463146
  event.notification.close();

  // This looks to see if the current is already open and
  // focuses if it is
  event.waitUntil(clients.matchAll({
    type: "window"
  }).then(function(clientList) {
    for (var i = 0; i < clientList.length; i++) {
      var client = clientList[i];
      if (client.url == '/' && 'focus' in client)
        return client.focus();
    }
    if (clients.openWindow)
      return clients.openWindow('/');
  }));

});
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Yogendra
  • 2,139
  • 3
  • 14
  • 27

1 Answers1

10

You should already have the subscription available on the pushManager object if you have already subscribed the user. So something like this should work:

registration.pushManager.getSubscription().then(function(subscription) {
  console.log("got subscription id: ", subscription.endpoint)
});

That's the whole endpoint, so if you just want the id you could get this:

subscription.endpoint.split("/").slice(-1))
Brendan Ritchie
  • 2,285
  • 15
  • 22
  • Specifically, `subscription.endpoint` is what you want in Chrome 44+, and in other browsers when they add support. If you care about Chrome 43 still, read http://updates.html5rocks.com/2015/03/push-notificatons-on-the-open-web#supporting-pre-chrome-44-push – Jeff Posnick Jul 08 '15 at 18:53
  • Right, thanks. Or I guess the id could be considered the final element of the endpoint. I've updated my answer. – Brendan Ritchie Jul 08 '15 at 19:13
  • registration.pushManager.getSubscription().then(function(subscription) { console.log("got subscription id: ", subscription.endpoint) }); above code's out put as follows in my case : https://android.googleapis.com/gcm/send – Yogendra Jul 09 '15 at 06:32
  • So somehow you got a subscription without a subscription id? I don't know how that's possible. Maybe something to do with the API configuration? – Brendan Ritchie Jul 09 '15 at 12:54
  • Actually if you're on an older version of chrome check whether there is anything else on that subscription object, like subscription.subscriptionId. – Brendan Ritchie Jul 09 '15 at 12:56
  • yeah!! that's work.....thanks. Actually i was just playing with chrome://serviceworker-internals/ and chrome://inspect/#service-workers and triggered push event from localhost. So possibly this could be reason i did't get API through subscription.endpoint – Yogendra Jul 09 '15 at 18:15
  • 1
    How can I get the subscriptionendpoint inside my service worker? – rat Nov 22 '15 at 01:57