如何从GCM规范ID [英] How to get Canonical ID from GCM

查看:314
本文介绍了如何从GCM规范ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得一个唯一的ID为我的设备,所以我可以从我的服务器推送通知。

I am trying to get a unique ID for my device so I can get push notifications from my server.

由于所有turorials说:我注册使用GMC:

As all turorials say : I register using GMC:

  GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
  String regid = gcm.register(PROJECT_NUMBER);  // <---- duplicated if uninstalled/ reinstalled
  SendRegIdToServer(regId);

现在,我送REGID我的服务器,节省设备。

Now, I send the regId my server and save on device.

这个问题是,当我卸载并重新安装,因为本地存储丢失,请问GCM再注册,我得到一个新的注册ID。正因为如此,我的服务器具有相同装置的重复。

The problem comes when I uninstall and reinstall, since the local storage is lost, I ask GCM to register again and I get a new registration ID. Because of this, my server is having duplicates of the same device.

我看着SO butlot的问题是在GCMRegistrar,这是德$ P $现在pcated。 人们说,使用规范的ID,它是独一无二的。但我怎么得到它呢?

I looked at SO butlot of questions are on GCMRegistrar, which is deprecated now. People say use Canonical ID, which is unique. But how do I get it ?

我使用gcm.register,并使用该ID,这显然是复制到服务器上。

I am using gcm.register and using that ID, which obviously is duplicating on the server.

鸭preciate任何帮助。

Appreciate any help.

推荐答案

当您发送邮件从服务器到谷歌的GCM服务器规范ID中返回的响应。

The Canonical id is returned in the response when you send a message from your server to google's gcm server.

<一个href="https://developer.android.com/google/gcm/http.html#response">https://developer.android.com/google/gcm/http.html#response

国米preting成功响应

Interpreting a success response

在一个JSON请求成功(HTTP状态code 200),响应   体内含有JSON对象具有以下字段:

When a JSON request is successful (HTTP status code 200), the response body contains a JSON object with the following fields:

字段说明multicast_id唯一ID(数字)标识   多播消息。消息成功数已处理   没有发生错误。这不可能是消息的失败数   处理。结果canonical_ids数包含规范   注册ID。见高级主题的本更多讨论   主题。对象的结果数组重新presenting的状态   邮件处理。对象中列出的相同的顺序的   请求(即,在请求的每个注册ID,其结果是   在响应中的相同的索引列出)并且它们能有这些   领域:与Message_ID:字符串重新presenting的消息时,它是   成功处理。 registration_id:如果设置,意味着GCM   处理的消息,但它还有另一个规范的注册ID为   该设备,让发送者应该取代对未来的请求的ID   (否则他们可能会被拒绝)。此字段从未设置,如果有   是在该请求的错误。错误:字符串描述一个错误,   在处理该收件人的消息时发生。可能   值如记载在上表相同,再加   不可用(意为GCM服务器是忙,无法处理   消息对于特定接收者,因此它可以被重试)。如果   失败和canonical_ids的值为0时,它没有必要解析   该反应的剩余部分。

Field Description multicast_id Unique ID (number) identifying the multicast message. success Number of messages that were processed without an error. failure Number of messages that could not be processed. canonical_ids Number of results that contain a canonical registration ID. See Advanced Topics for more discussion of this topic. results Array of objects representing the status of the messages processed. The objects are listed in the same order as the request (i.e., for each registration ID in the request, its result is listed in the same index in the response) and they can have these fields: message_id: String representing the message when it was successfully processed. registration_id: If set, means that GCM processed the message but it has another canonical registration ID for that device, so sender should replace the IDs on future requests (otherwise they might be rejected). This field is never set if there is an error in the request. error: String describing an error that occurred while processing the message for that recipient. The possible values are the same as documented in the above table, plus "Unavailable" (meaning GCM servers were busy and could not process the message for that particular recipient, so it could be retried). If the value of failure and canonical_ids is 0, it's not necessary to parse the remainder of the response.

更新

下面是典型的ID一些更多的信息。基本上,如果莫名其妙的设备寄存器ID变成了什么谷歌认为它应该是,那么当你的服务器发送带有不同步的ID请求同步时,GCM服务器将包括在它的响应,正确的ID是在未来使用。

Below is some more info on Canonical IDs. Basically, if somehow the device reg id becomes out of sync with what Google thinks it should be, then when your server sends a request with the out-of-sync id, the gcm server will include in it's response , the correct id to be used in the future.

想想看,这种工作方式是;您的服务器应该已经保存了reg ID时,该设备注册GCM。您的服务器将请求发送到GCM与ID,GCM使用该ID发送消息到您的设备。 GCM无法在设备上的寄存器ID没有告诉它的服务器。如果它没有你的服务器将只保留发送了错误的寄存器ID。相反,GCM告诉它使用特定设备的寄存器ID是坏的服务器,您的服务器就可以将消息发送到设备更新其存储的寄存器ID来它应该是什么,然后该设备可以ACK的变化在REG标识。下面的信息暗示有一定的时间限制为过多久,坏的ID仍然可以用来发送消息。我认为,假设是,它应该是足够长的时间你的服务器(使用坏的ID通过GCM消息)更改设备ID

Think about it, the way this works is; your server should have stored the reg id when the device registered with gcm. Your server sends a request to gcm with that id, gcm uses that id to send a message to your device. GCM can't the reg id on your device without telling the server about it. If it did your server would just keep sending the wrong reg id. Instead, gcm tell the server that the reg id it is using for a particular device is bad, your server can then send a message to the device to update its stored reg id to what it should be, and then the device can ack the change in REG IDs. The info below implies there is some time limit as too how long the "bad" id can still be used to send messages. I think the assumption is that it should be long enough for your server to change the device id (via a gcm message using the "bad" id)

典型的ID

在服务器端,只要应用程序表现很好,一切都应该正常工作。但是,如果在应用程序中的错误触发多个登记为同一台设备,它可以是难以调和的状态,你可能最终与重复的消息。

On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.

GCM提供了一个名为规范注册的ID来轻易地从这些情况中恢复设施。一个规范的注册ID被定义为您的应用程序所要求的最后注册的ID。这是将消息发送到设备时,服务器应使用的ID。

GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application. This is the ID that the server should use when sending messages to the device.

如果您稍后尝试使用不同的注册ID发送消息,GCM将处理该请求像往常一样,但它会在响应的registration_id领域的规范注册ID。请务必将储存在你的服务器,这个规范ID的注册ID,因为最终你使用会停止工作的ID。

If later on you try to send a message using a different registration ID, GCM will process the request as usual, but it will include the canonical registration ID in the registration_id field of the response. Make sure to replace the registration ID stored in your server with this canonical ID, as eventually the ID you're using will stop working.

这篇关于如何从GCM规范ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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