2013-04-29 79 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

好的謝謝,所以在那一刻我會通知第三方服務器的REGID刪除?如果我只是從設備中刪除應用程序sholdn't我的[編輯1]中的代碼執行在某些點可以通過REGID到一個PHP文件,從DB刪除REGID? – turtleboy 2013-04-29 13:04:44

+0

不客氣。你沒有。您無法知道應用何時被卸載,因此您無法知道何時調用該代碼。您應該調用此代碼的唯一情況是,如果您有一種方案要停止將GCM消息發送到應用程序,即使它仍安裝在設備上。如果應用程序已卸載,則第三方服務器會在嘗試向該設備發送消息後收到「NotRegistered」錯誤時知道刪除該REGID。 – Eran 2013-04-29 13:17:30

+0

嗨,我明白你在說什麼,但我仍然在黑暗中感受到了我所遵循的教程。在我看來,我認爲在教程中可能存在缺少從第三方服務器的數據庫中刪除REGID的文件。你說GCM服務器告訴第三方服務器從數據庫中刪除REGID,但沒有相應的PHP文件可以做到這一點。如果你有時間可以快速瀏覽一下php文件,也許可以驗證我在考慮丟失的非註冊文件 – turtleboy 2013-04-29 14:07:10