2013-04-29 16 views
11

GCMプッシュ通知を使用するアプリがあります。それは正常に動作し、私のデバイスはプッシュメッセージを登録し、受信します。GCM GCMとサードパーティのサーバーでデバイスを登録解除する方法

私のデバイスからアプリケーションをアンインストールすると、私はもはや期待どおりにメッセージを受信しません。あなたが期待していたアプリをアンインストールした後も、あなたがサーバー上でメッセージを送信するTextBoxはまだそこにあります。

私は登録解除に関する文書を見てきました。手動または自動でそれを行うことができます。

The end user uninstalls the application. 
The 3rd-party server sends a message to GCM server. 
The GCM server sends the message to the device. 
The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false. 
The GCM client informs the GCM server that the application was uninstalled. 
The GCM server marks the registration ID for deletion. 
The 3rd-party server sends a message to GCM. 
The GCM returns a NotRegistered error message to the 3rd-party server. 
The 3rd-party deletes the registration ID. 

私は上のリストの最後のステートメントを理解していません。

The GCM returns a NotRegistered error message to the 3rd-party server. 

これはどのように達成されましたか?

また、アプリがデバイスからアンインストールされている場合、以下のステートメントをどのように行うことができますか?アプリケーションがデバイスから削除されたときに実行されるアプリライフサイクルメソッドはありますか?存在する場合は、GCMサーバにアンインストールを通知するコードが置かれている場所で、DBからregIDを削除するサードパーティサーバ上のphpスクリプトを呼び出しますか?事前に

The GCM client informs the GCM server that the application was uninstalled. 

おかげで、

マット

[edit1] 

static void unregister(final Context context, final String regId) { 
     Log.i(TAG, "unregistering device (regId = " + regId + ")"); 
     String serverUrl = SERVER_URL + "/unregister.php"; 
     Map<String, String> params = new HashMap<String, String>(); 
     params.put("regId", regId); 
     try { 
      post(serverUrl, params); 
      GCMRegistrar.setRegisteredOnServer(context, false); 
      String message = context.getString(R.string.server_unregistered); 
      CommonUtilities.displayMessage(context, message); 
     } catch (IOException e) { 
      // At this point the device is unregistered from GCM, but still 
      // registered in the server. 
      // We could try to unregister again, but it is not necessary: 
      // if the server tries to send a message to the device, it will get 
      // a "NotRegistered" error message and should unregister the device. 
      String message = context.getString(R.string.server_unregister_error, 
        e.getMessage()); 
      CommonUtilities.displayMessage(context, message); 
     } 
    } 

[EDIT2] 下の登録解除コードを携帯電話からアプリを削除した後、サードパーティのサーバー上のデバイスの登録を解除するためにあります。このコードは以下のチュートリアルに追加されています。

tutorial

send_messages.php

<?php 
if (isset($_GET["regId"]) && isset($_GET["message"])) { 
    $regId = $_GET["regId"]; 
    $message = $_GET["message"]; 
    $strRegID = strval($regId); 

    include_once './GCM.php'; 
    include_once './db_functions.php'; 
    $gcm = new GCM(); 

    $registatoin_ids = array($regId); 
    $message = array("price" => $message); 

    $result = $gcm->send_notification($registatoin_ids, $message); 
    $db = new db_Functions(); 

    if (strcasecmp (strval($result) , 'NotRegistered')) { 
    $db->deleteUser($strRegID); 
    } 
} 
?> 

db_functions.php

public function deleteUser($regid) { 

    $strRegID = strval($regid); 

    $serverName = "LOCALHOST\SQLEXPRESS"; 
     $uid = "gcm";  
     $pwd = "gcm";  
     $databaseName = "gcm"; 

     $connectionInfo = array("UID"=>$uid, "PWD"=>$pwd, "Database"=>$databaseName); 


      $db = sqlsrv_connect($serverName,$connectionInfo) or die("Unable to connect to server"); 

      $query = "DELETE FROM gcmUser2 WHERE gcuRegID = '$regid'"; 
      $result = sqlsrv_query($db, $query); 


    } 

答えて

14

GCMサーバがGCM、アプリケーションがアンインストールされた後にデバイスにメッセージを送信しようとするとクライアントは、このアプリがデバイスにインストールされていないことを検出しました。あなたはアプリケーションコードでそれをしません。 Android OSのGCMクライアントコンポーネントがそれを行います。

次回、アンインストールしたデバイスのアプリにメッセージを送信しようとすると、GCMサーバは既にアンインストールされていることを知り、NotRegisteredエラーを送信します。

アプリがデバイスから削除されたときに呼び出されるライフサイクルメソッドはありません。 GCMサーバーとサードパーティのサーバーでアプリがアンインストールされたことを検出できるようにするには、上で引用した一連のイベントは必要ありません(アプリをアンインストールしたGCMサーバーを開き、そのデバイスからアプリがアンインストールされたことをサードパーティのサーバーに知らせる)。

+0

okありがとうございます。どの時点で、私は第三者サーバーにregIDを削除するよう通知しますか?私はちょうど私の[編集1]のコードは、DBからregIDを削除するPHPファイルにregIDを渡すことができるいくつかの時点で実行するsholdn'tデバイスからアプリケーションを削除する場合は? – turtleboy

+0

ようこそ。あなたはそうしない。いつあなたはそのアプリをアンインストールするのかわからないので、そのコードをいつ呼び出すべきかわからない。このコードを呼び出す必要がある唯一のケースは、まだデバイスにインストールされている場合でもアプリにGCMメッセージを送信しないようにするシナリオがある場合です。アプリケーションがアンインストールされている場合、サードパーティのサーバは、そのデバイスにメッセージを送信しようとした後に 'NotRegistered'エラーが発生したときにregIDを削除することを知ります。 – Eran

+0

こんにちは、あなたが何を言っているのか理解していますが、私はまだ暗闇の中で私が行ったチュートリアルについて少し感じています。私の意見では、私は、第三者サーバーのDBからのregIDの削除を処理するチュートリアルに欠けているファイルがあると思います。 GCMサーバーがサードパーティのサーバーにDBからregIDを削除するように指示していますが、これを行うことができる対応するPHPファイルはありません。あなたは時間がある場合は、PHPファイルを簡単に見て、おそらく私は不明な登録ファイルに関する考えていることを確認することができます – turtleboy

関連する問題