1

我使用 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);
  });
});

我的扩展程序正在与 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);
4

1 回答 1

1

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

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

于 2016-09-23T07:28:01.263 回答