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