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

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

问题描述

我在数据库表中有一个 GCM 注册用户及其相应注册 ID 的列表,我实际上想在用户从表中删除时取消注册.我在 Stackoverflow 中找到了很多示例,但其中大部分基于旧的 GCMRegistrar API,现在已弃用.我正在使用 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());
        }
}

我有一个管理员应用程序,它充当第 3 方应用程序服务器,因为它将通知推送给所有用户.我想使用以下方法从该管理员应用中取消注册特定用户:

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

   }

但是让我困惑的是 unregister() 方法没有将注册 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 ?.

推荐答案

这个方法可以解决问题:

This method does the trick:

gcm.unregister();

但是,现在不推荐使用 gcm.unregister(),因此,您必须使用以下之一:

However, gcm.unregister() is now deprecated, hence, you must use one of these:

InstanceID.deleteToken() 或 InstanceID.deleteInstanceID().

InstanceID.deleteToken() or InstanceID.deleteInstanceID().

这些方法采用以下参数:

These methods take the following parameters:

public void deleteToken (String authorizedEntity, String scope)

被授权实体是您要删除的实体...

Being authorized entity the entity that you want to remove...

//以下说明仅适用于取消注册"

// THE FOLLOWING EXPLANATION IS ONLY FOR "UNREGISTER"

因此,根据您的评论:

但是让我困惑的是 unregister() 方法不接受注册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.

那是因为您期望它以一种它没有的方式工作.似乎您希望能够发出一个将参数传递给 uregister 的 http 请求(例如 "http://www.gcmserver.com?unregisterid=xxxx"),这不是它的工作方式,这是基于 Google 文档的工作方式:

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.

第3方服务器向GCM服务器发送消息.

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

GCM 服务器将消息发送到设备.

The GCM server sends the message to the device.

GCM 客户端收到消息并查询包管理器是否有配置接收它的广播接收器,其中返回假.

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.

第 3 方服务器向 GCM 发送消息.

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

GCM 向第 3 方服务器返回 NotRegistered 错误消息.

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

第三者删除注册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.

问候!

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

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