GCM如何注销与GCM和第三方服务器的设备 [英] GCM how to unregister a device with GCM and 3rd party server

查看:367
本文介绍了GCM如何注销与GCM和第三方服务器的设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果卸载从我的设备中的软件,我不会再收到消息,因为你所期望的。在你的服务器上发送消息的文本框后仍然有我卸载应用程序,这也是我所期望的。

我已经看了关于注销的文档,你可以手动或自动做到​​这一点。

 最终用户卸载应用程序。
在第三方服务器发送消息给GCM服务器。
的GCM服务器将消息发送到该设备。
该GCM客户端接收关于是否有配置为接收它,它返回false广播接收机信息和查询包管理器。
该GCM客户端通知应用程序被卸载GCM服务器。
在GCM服务器标记为删除的注册ID。
在第三方服务器发送消息给GCM。
该GCM返回NotRegistered错误信息给第三方服务器。
该第三方删除注册ID。
 

我不明白,在上面的列表中的倒数第二个发言。

 的GCM返回NotRegistered错误信息给第三方服务器。
 

这是如何来达到的?

另外,如果应用程序是从设备卸载,怎么能做到下面的语句?是否有执行的一个应用程序从设备中删除的应用程序生命周期的方法?如果有,是这样的,通知卸载的GCM服务器和调用,删除REGID从数据库的第三方服务器上的PHP脚本,其中code放置的地方?

 的GCM客户端通知应用程序卸载的GCM服务器。
 

在此先感谢,

  

静态无效的注销(最终上下文的背景下,最终的字符串REGID){
        Log.i(TAG,取消注册装置(REGID =+ REGID +));
        字符串的serverUrl = SERVER_URL +/unregister.php;
        地图<字符串,字符串> PARAMS =新的HashMap<字符串,字符串>();
        params.put(REGID,REGID);
        尝试 {
            后期(的serverUrl,则params);
            GCMRegistrar.setRegisteredOnServer(背景下,假);
            字符串消息= context.getString(R.string.server_unregistered);
            CommonUtilities.displayMessage(背景下,消息);
        }赶上(IOException异常E){
            //此时该装置是从GCM未登记,但仍
            //在服务器中注册。
            //我们可以尝试再次取消注册,但它不是必需的:
            //如果服务器尝试发送一个消息到设备,它会得到
            //一个NotRegistered的错误消息,应注销该设备。
            字符串消息= context.getString(R.string.server_unregister_error,
                    e.getMessage());
            CommonUtilities.displayMessage(背景下,消息);
        }
    }
 

在注销code以下是取消注册的第三方服务器上的设备从手机中删除应用程序之后。在code是除了下面的教程。

<一个href="http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/">tutorial

send_messages.php

 &LT; PHP
如果(使用isset($ _ GET [REGID])及和放大器;使用isset($ _ GET [信息])){
    $ REGID = $ _GET [REGID];
    $消息= $ _GET [信息];
    $ strRegID = strval($ REGID);

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

    $ registatoin_ids =阵列($ REGID);
    $消息=阵列(价格=&GT; $消息);

    $结果= $ gcm-&GT; send_notification($ registatoin_ids,$消息);
    $ DB =新db_Functions();

    如果(strcasecmp(strval($结果),NotRegistered')​​){
    $ DB-&GT; deleteUser($ strRegID);
    }
}
?&GT;
 

db_functions.php

 公共职能deleteUser($ REGID){

    $ strRegID = strval($ REGID);

    $的serverName =本地主机\ SQLEX $ P $干燥综合征;
        $ UID =GCM;
        $ PWD =GCM;
        $ DATABASENAME =GCM;

        $ connectionInfo =阵列(UID=&GT; $ UID,PWD=&GT; $ PWD数据库=&GT; $数据库名);


             $ DB = sqlsrv_connect($ servername,请将$ connectionInfo)或死亡(无法连接到服务器);

             $查询=DELETE FROM gcmUser2 WHERE gcuRegID ='$ REGID';
             $结果= sqlsrv_query($分贝,$查询);


    }
 

解决方案

在GCM服务器尝试发送邮件到应用程序已被卸载后,在设备上,GCM客户端检测到这个程序是不是再在设备上安装。您没有在应用程序中code做到这一点。 Android操作系统的GCM客户端组件做的。

下一次尝试发送一个信息,即卸载它在设备上的应用程序,在GCM服务器将已经知道它已被卸载,并送你 NotRegistered 错误。

有没有当应用程序从设备中删除所谓的生命周期方法。如果有,你就不需要你上面,以便报价为GCM服务器和第三方服务器事件,以检测该应用程序卸载(因为你也可以使用这样的方法,以两者的顺序注销从您的应用程序GCM服务器,并让第三方服务器知道该应用程序从该设备卸载)。

I've an app that uses GCM push notifications. It works fine and my device registers and receives push messages.

If i uninstall the app from my device i no longer receive the messages as you would expect. The TextBox in which you send messages on the server is still there after i un-install the app, which i'd also expect.

I've looked at the documentation regarding unregistering and you can do it manually or automatically.

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.

I don't understand the next to last statement in the above list.

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

How is this acheived?

Also if the app is uninstalled from the device, how can it do the statement below? Is there an app lifecycle method that execute as an app is removed from a device? If there is, is this the place where code is placed that informs GCM server of the uninstall and the calls a php script on the 3rd party server that removes the regID from the DB?

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

thanks in advance,

Matt

[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] The unregister code below is for unregistering the device on the 3rd party server after deleting the app from the phone. The code is in addition to the tutorial below.

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);


    }

解决方案

When the GCM server attempts to send the message to the device after the app has been uninstalled, the GCM client detects that this app is not longer installed on the device. You don't do it in your application code. The GCM client component of the Android OS does it.

The next time to try to send a message to the app on the device that uninstalled it, the GCM server will already know it has been uninstalled, and send you the NotRegistered error.

There is no lifecycle method called when the app is removed from the device. If there was, you wouldn't need the sequence of events you quoted above in order for the GCM server and the 3rd party server to detect that the app was uninstalled (since you could have used such a method to both unregister your app from the GCM server and to let the 3rd party server know the app was uninstalled from that device).

这篇关于GCM如何注销与GCM和第三方服务器的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆