从注销GCM在Android中使用注册ID的设备 [英] Unregister a device from GCM using registration Id in Android

查看:235
本文介绍了从注销GCM在Android中使用注册ID的设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有GCM列表注册用户和其相应的注册ID的数据库中的表,其实我是想注销时,他/她是从表中删除用户。我发现很多的例子在这里#1,但其中大部分都是基于老GCMRegistrar API,它现在是很precated。我使用GoogleCloudMessaging API和通过下面的方法注册用户:

I have a list of GCM registered users and their corresponding registration Ids in a database table, and I actually want to unregister a user whenever he/she is deleted from the table. I found a lot of examples here in Stackoverflow, but most of them are based on the old GCMRegistrar API which is now deprecated. I'm using GoogleCloudMessaging API and registering a user by following method:

private void registerUser(){
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getBaseContext());
        String regId = "";
        try {
            regId = gcm.register(getString(R.string.project_number));
            Log.i("registrationId", regId);
        } 
        catch (IOException e) {
            Log.i("Registration Error", e.getMessage());
        }
}

我有一个管理员应用程序,它作为一个第三方的应用程序服务器,因为它推向所有用户的通知。我想从注销该管理员应用程序的特定用户使用下面的方法:

I have an administrator app, which acts as a 3rd-party application server, since it pushes notifications to all users. I want to unregister a specific user from this administrator app with following method:

private void unRegister(String regId) {

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(getBaseContext());
        try {
            gcm.unregister();
        } 
        catch (IOException e) {     
        System.out.println("Error Message: " + e.getMessage());
        }

   }

但它混淆了我,注销()方法并不需要注册ID作为一个参数,这使得它无法注销特定的设备。有没有办法通过注册ID注销从GCM特定的设备?

But it confuses me that unregister() method does not take registration Id as an argument, which makes it impossible to unregister a specific device. Is there any way to unregister a specific device from GCM by registration Id ?.

推荐答案

该方法是卓有成效的:

gcm.unregister();

因此​​,基于您的评论:

So, based on your comment:

但它混淆了我,注销()方法并不需要注册   ID作为参数,这使得它无法注销特定   设备。

But it confuses me that unregister() method does not take registration Id as an argument, which makes it impossible to unregister a specific device.

那是因为你期望它,因为它没有办法工作。好像你希望能够做一个HTTP请求将参数传递给uregister(如 http://www.gcmserver.com ?unregisterid = XXXX ),这不是它的工作方式,这是它的工作原理基于谷歌文档的方式:

That's because you are expecting it to work in a way that it doesn't. It seems like you want to be able to make an http request passing a parameter to uregister(e.g. "http://www.gcmserver.com?unregisterid=xxxx"), and that's not the way it works, this is the way it works based on Google's Documentation:

如何注销作品

这是应用程序可以后会自动取消注册   从设备卸载。然而,这种方法不会发生   马上,由于Android没有提供卸载的回调。什么   发生在这个方案中,如下所示:

An application can be automatically unregistered after it is uninstalled from the device. However, this process does not happens right away, as Android does not provide an uninstall callback. What happens in this scenario is as follows:

最终用户卸载应用程序。

The end user uninstalls the application.

在第三方服务器发送消息给GCM服务器。

The 3rd-party server sends a message to GCM server.

的GCM服务器将消息发送到该设备。

The GCM server sends the message to the device.

在GCM客户端接收有关信息和查询程序包管理器   是否有广播接收机配置为接收它,这   返回false。

The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false.

在GCM客户端通知GCM服务器的   申请被卸载。

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

在GCM服务器标记为删除的注册ID。

The GCM server marks the registration ID for deletion.

在第三方服务器发送消息给GCM。

The 3rd-party server sends a message to GCM.

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

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

该第三方删除注册ID。

The 3rd-party deletes the registration ID.

因此​​,基于这,有什么方法gcm.unregister()实际上做的是标志着该设备为删除(认为它作为强制过程的第一步实际上不卸载应用程序),让服务器知道它不再需要得到通知,也不采取一个id作为参数这意味着它引用到该特定设备。

So, based on that, what the method gcm.unregister() actually does is marking that device for deletion(think of it as forcing the first steps of the process without actually uninstalling the app), letting the server know that it no longer needs to get notifications, also by not taking an "Id" as a parameter it means that it is referencing to that specific device.

商祺!

这篇关于从注销GCM在Android中使用注册ID的设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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