1

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トークンを登録解除する方法です。クロムエクステンションにアンインストールイベントはありません。クロムエクステンションに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

。とにかく通知がユーザーに届かない場合は、なぜ登録を解除する必要がありますか? –

+1

こんにちは、お返事ありがとうございます。クロムエクステンションをインストールして、GCM IDが作成しているインストールごとに自分の拡張機能をテストするために何度もアンインストールしました(10回インストールすると10個のGCM IDが作成されます)。ユーザーに通知するたびに、GCM IDが登録されていないため、10個の通知が届いていました。私はどこに登録解除を呼び出すかわからない私の拡張機能。 – Java4you

+0

@ Java4you私はまったく同じ問題を扱っています:)これに対する解決策を見つけましたか? –

答えて

1

アプリがGCMサービスから自動的に登録解除されます。イベントリスナーを持つ必要はありません!

ソース - https://developers.google.com/cloud-messaging/chrome/clientあなたは、外部のサーバからそれを行うに役立つかもしれないsetUninstallUrlがある

+0

あなたのアプリケーションや拡張機能はgcm.unregisterを呼び出して登録トークンを取り消すことができます。登録解除は、アプリや拡張機能がそれ以上のメッセージを受信したくない場合や、登録トークンが侵害されている可能性がある場合など、まれにしか行われません。 – Java4you

関連する問題