1

我使用GCM向用戶瀏覽器發送通知。要註冊我使用的GCM服務如何在Chrome擴展中註銷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); 
+0

有setUninstallUrl這可能會幫助你從外部服務器做到這一點。現在,爲什麼你需要取消註冊,如果通知不會到達用戶呢? –

+1

嗨,謝謝你的回覆。我安裝了我的Chrome擴展程序並卸載了很多次,以便在我的PC中測試每個GCM ID正在創建的每個安裝(我安裝的10次意味着創建了10個GCM ID)的擴展。每當我向用戶發送通知時,我都會收到10條通知,因爲我的GCM ID未被註冊。我不知道在哪裏調用取消註冊功能我的擴展。 – Java4you

+0

@ Java4you我正在處理完全相同的問題:)你有沒有找到這個解決方案? –

回答

1

您的應用程序將被自動取消註冊在GCM服務。不需要有事件監聽器!

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

+0

您的應用或擴展程序可能會調用gcm.unregister來撤消註冊令牌。註銷應僅在極少數情況下完成,例如,如果您的應用或分機不想接收更多的消息,或者註冊令牌被懷疑受損。 – Java4you