android - GCM 如何注销设备与 GCM 和第 3 方服务器

标签 android push-notification google-cloud-messaging

我有一个使用 GCM 推送通知的应用程序。它工作正常,我的设备注册并接收推送消息。

如果我从我的设备上卸载该应用程序,我将不再像您期望的那样收到消息。在我卸载应用程序后,您在服务器上发送消息的文本框仍然存在,这也是我所期望的。

我查看了有关注销的文档,您可以手动或自动执行。

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 服务器卸载的代码的地方,并在第 3 方服务器上调用从数据库中删除 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);
        }
    }

[编辑2] 下面的注销代码用于从手机中删除应用程序后在第 3 方服务器上注销设备。该代码是对下面教程的补充。

tutorial

发送消息.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);


    }

最佳答案

当 GCM 服务器在应用程序卸载后尝试向设备发送消息时,GCM 客户端检测到该应用程序不再安装在设备上。您不会在应用程序代码中执行此操作。 Android 操作系统的 GCM 客户端组件会执行此操作。

下次尝试向卸载它的设备上的应用程序发送消息时,GCM 服务器将已经知道它已被卸载,并向您发送 NotRegistered 错误。

当应用程序从设备上移除时,没有调用生命周期方法。如果有,您将不需要上面引用的事件序列来让 GCM 服务器和第三方服务器检测到该应用程序已卸载(因为您可以使用这种方法从GCM 服务器并让第 3 方服务器知道该应用已从该设备上卸载)。

关于android - GCM 如何注销设备与 GCM 和第 3 方服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16278977/

相关文章:

android - 我在我的应用程序中使用 Google Cloud Messaging 推送通知,对此我有 2 个基本疑问

android - 使用 Google Cloud Messaging 云连接服务器 (XMPP) 的上游文件

android - 在android studio中导入类

android - 无法将提供的符号转换为文件或 URI

android - 我如何获得我的注册 ID 设备

java - xmpp openfire 推送通知和日志记录

ios - 如何无限每 1 分钟运行一个 iOS 应用程序?

android - 在解析中,发送的推送始终为 0

android - 在 ActionBar.Tab fragment 中显示 DialogFragment

java - Android/Java 试图找出放置变量声明的位置