继GCM: Getting Started指南中的最后一节后,在收到结果后需要做一些记录。GCM:MulticastResult - 哪个结果来自哪个设备?
从导引用:
它现在有必要来分析结果,并采取在下列情况下采取适当行动:
- 如果在创建消息,但结果返回的规范登记ID,有必要用当前的注册号码替换当前的注册号码
ID。- 如果返回的错误是NotRegistered,则需要删除该注册ID,因为该应用程序已从设备上卸载 。
这里的一个代码段用于处理这些2个条件:
if (result.getMessageId() != null) { String canonicalRegId = result.getCanonicalRegistrationId(); if (canonicalRegId != null) { // same device has more than on registration ID: update database } } else { String error = result.getErrorCodeName(); if (error.equals(Constants.ERROR_NOT_REGISTERED)) { // application has been removed from device - unregister database } }
引导上述指单个结果,而不是多播的情况。 我不知道如何处理多播情况:
ArrayList<String> devices = new ArrayList<String>();
for (String d : relevantDevices) {
devices.add(d);
}
Sender sender = new Sender(myApiKey);
Message message = new Message.Builder().addData("hello", "world").build();
try {
MulticastResult result = sender.send(message, devices, 5);
for (Result r : result.getResults()) {
if (r.getMessageId() != null) {
String canonicalRegId = r.getCanonicalRegistrationId();
if (canonicalRegId != null) {
// same device has more than on registration ID: update database
// BUT WHICH DEVICE IS IT?
}
} else {
String error = r.getErrorCodeName();
if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
// application has been removed from device - unregister database
// BUT WHICH DEVICE IS IT?
}
}
}
} catch (IOException ex) {
Log.err(TAG, "sending message failed", ex);
}
我提交设备的列表,并接收返回结果的列表。 Result对象不包含注册标识,但只包含一个规范标识,如果第一个过时。 如果两个列表是共同关联的(即保留订单和大小),则它是无证的。
如何确定哪个结果指哪个设备?
- UPDATE
我粘贴的解决方案的一个片段如下
谢谢!我刚刚在GCM google群组中找到了此信息(https://groups.google.com/forum/#!话题/ Android的GCM/DCHHQwqTs8M)。新的API需要时间才能获得正确的文档。 – auval 2012-07-21 20:50:59
谢谢!问自己同样的问题! – 2012-08-23 14:13:05