如何在chrome扩展中取消注册gcm

时间:2016-09-22 07:01:59

标签: google-chrome google-chrome-extension google-cloud-messaging chrome-gcm

我使用GCM向用户浏览器发送通知。要注册我使用的GCM服务

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正在与GCM建立连接,我正在向用户浏览器发送通知。我的问题是如何在用户卸载扩展时取消注册GCM令牌。 chrome扩展程序中没有卸载事件。你能不能请任何人告诉我在我的chrome扩展程序中取消注册GCM连接代码的位置。

在我的扩展程序中编写此代码的位置(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 个答案:

答案 0 :(得分:1)

当用户卸载时,您的应用将自动从GCM服务注销。无需为它设置事件监听器!

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