How to unregister gcm in chrome extension

572 Views Asked by At

I used GCM to send notifications to user browsers. To register GCM service I used

function registerCallback(registrationId) {
  if (chrome.runtime.lastError) {
    // When the registration fails, handle the error and retry the
    // registration later.
    return;
  }

  // Send the registration token to your application server.
  sendRegistrationId(function(succeed) {
    // Once the registration token is received by your server,
    // set the flag such that register will not be invoked
    // next time when the app starts up.
    if (succeed)
      chrome.storage.local.set({registered: true});
  });
}

function sendRegistrationId(callback) {
  // Send the registration token to your application server
  // in a secure way.
}

chrome.runtime.onStartup.addListener(function() {
  chrome.storage.local.get("registered", function(result) {
    // If already registered, bail out.
    if (result["registered"])
      return;

    // Up to 100 senders are allowed.
    var senderIds = ["Your-Sender-ID"];
    chrome.gcm.register(senderIds, registerCallback);
  });
});

My Extension is getting connection with GCM and I am sending notifications to user browsers. My Question is How to unregister GCM token when user uninstall extension. There is no uninstall event in chrome extension. Could you please any one tell me where to write unregister GCM Connection code in my chrome extension.

Where to write this code in my extension (background.js, contentscript.js)..

function unregisterCallback() {
  if (chrome.runtime.lastError) {
    // When the unregistration fails, handle the error and retry
    // the unregistration later.
    return;
  }
}

chrome.gcm.unregister(unregisterCallback);
1

There are 1 best solutions below

1
adjuremods On

Your app will be automatically unregistered from the GCM service when the user uninstalls it. No need to have an event listener for it!

source - https://developers.google.com/cloud-messaging/chrome/client