当前位置: 首页 > 知识库问答 >
问题:

GCM如何使用GCM和第三方服务器注销设备

施德运
2023-03-14

我有一个应用程序,使用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服务器卸载,并在第三方服务器上调用php脚本,从数据库中删除regID?

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]下面的注销代码用于从手机中删除应用程序后在第三方服务器上注销设备。该代码是对以下教程的补充。

辅导的

发送消息。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_函数。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);


    }

共有1个答案

巫马盛
2023-03-14

卸载应用程序后,当GCM服务器尝试向设备发送消息时,GCM客户端检测到设备上不再安装此应用程序。在应用程序代码中不需要这样做。Android操作系统的GCM客户端组件完成了这一任务。

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

从设备中删除应用程序时,没有调用生命周期方法。如果有,GCM服务器和第三方服务器不需要按照上面引用的事件顺序来检测应用程序是否已卸载(因为您可以使用这种方法从GCM服务器注销应用程序,并让第三方服务器知道应用程序已从该设备卸载)。

 类似资料:
  • 我在一个数据库表中有一个GCM注册用户列表及其相应的注册ID,我实际上想在用户从表中删除时注销该用户。我在Stackoverflow中发现了很多示例,但大多数示例都基于旧的GCMRegistarAPI,现在已经不推荐使用了。我正在使用GoogleCloudMessaging API,并通过以下方法注册用户: 我有一个管理员应用程序,它充当第三方应用程序服务器,因为它向所有用户推送通知。我想注销一个

  • 我制作了一个Android应用程序。我使用GCM推送通知。当用户登录时,我正在向GCM注册设备。我使用外部MySql数据库来存储用户的注册ID。它工作得很好。 但是 当我做了以下步骤: 在我的手机上安装了我的应用程序。 用用户1登录。 从我的手机卸载应用程序。 再次安装应用程序。 用用户2登录。 我的手机仍会收到user1通知,这意味着卸载应用程序时GCM没有注销我的设备。 我不能删除的应用程序卸

  • 我已经从中看到了“GCM体系结构概述”http://developer.android.com/guide/google/gcm/gcm.html.文档说,当android应用程序注册GCM时,会调用onRegistered(上下文c,字符串regId),其中包含regId,然后用户将该regId发送到其服务器。该文件称“谷歌可能会定期刷新注册ID”。我的问题是,我怎么知道谷歌已经为用户刷新了re

  • 我尝试使用GCM发送APN。设备注册OK,GCM和IOS设备令牌被接收。当我尝试发送有效载荷没有"通知"节点: 一切正常。 但是我想处理应用程序在后台时的通知 根据文档,我将有效负载修改为: 第一次尝试: 但没有收到通知。 第二次尝试: 然后应用程序不会收到任何通知,即使我删除了通知节点。所以我必须移除它,重新安装,接收新的代币。 第二期: 我试图检查APN使用NWPusher,但得到错误:无效令

  • 问题内容: 我已经在我的应用中实现了GCM通知。我现在尝试在用户注销时注销该应用程序。我正在使用以下代码。执行此代码时,它将导致应用程序崩溃,并显示以下logcat: 这是代码: 问题答案: 将支持库更新为25.0.0后,我遇到了同样的问题。对我来说,更新下面的库之后,在应用程序gradle文件中,问题消失了。

  • 你好,我正在尝试在我的Android应用程序上使用GCM。 我的问题是,当我试图发送消息时,我得到401错误,未经授权的错误。 我在谷歌云控制台中为服务器应用程序制作了一个密钥,并遵循以下说明:http://developer.android.com/google/gcm/gs.html 这是我的代码: 从我的应用程序,我发送了POST到这个PHP代码: