Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCM registration id in Service Worker in Push Notification for chrome

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('/');
  }));

});
like image 420
Yogendra Avatar asked Jul 08 '15 07:07

Yogendra


People also ask

What is GCM registration ID?

Google Cloud Messaging (GCM) Sender ID: A unique numerical value which is created when you configure your Project in the Google Developers Console/ Google Cloud Console.

How does GCM push notification work?

The first step in GCM is that a third-party server (such as an email server) sends a request to Google's GCM server. This server then sends the message to your device, through that open connection. The Android system looks at the message to determine which app it's for, and starts that app.

What is APN and GCM?

From this thread, GCM is a service that helps developers send data from servers to their Android applications on Android devices. Same with APN which is a service for app developers to propagate information to iOS (and, indirectly, watchOS), tvOS, and macOS devices.


1 Answers

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))
like image 54
Brendan Ritchie Avatar answered Nov 13 '22 13:11

Brendan Ritchie