GCM:MulticastResult - 哪个结果来自哪个设备?

Ami*_*val 31 android google-cloud-messaging

GCM:入门指南的最后一部分之后,在收到结果后需要完成一些簿记.

引言:

现在有必要解析结果并在以下情况下采取适当的措施:

  • 如果消息已创建但结果返回了规范的注册ID,则必须将当前注册
    ID替换为规范注册ID.
  • 如果返回的错误是NotRegistered,则必须删除该注册ID,因为该应用程序已从设备中卸载.

这是一个处理这两个条件的代码片段:

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
 }
}
Run Code Online (Sandbox Code Playgroud)

上面的指南是指单个结果,而不是指多播情况.我不知道如何处理多播案例:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

我提交了一份设备列表,并收到了一份结果列表.Result对象不包含注册ID,但如果第一个是过时的,则只包含规范ID.如果两个列表是相关的(即保留顺序和大小),则它没有记录.

我怎样才能确定哪个结果涉及哪个设备?

- 更新

我在下面的单独答案中粘贴了一小段解决方案

azg*_*fer 21

结果按您发送到GCM服务器的registration_id数组的顺序排列.例如,如果您的registration_ids是:

[id1, id4, id7, id8]
Run Code Online (Sandbox Code Playgroud)

然后,您获得的结果数组将具有id1,id4,id7和id8的相同顺序.

您只需要相应地解析每个结果,例如,如果第二个结果具有'message_id'和'id9'的'registration_id',您知道'id4'现在已经过时,应该被id9替换.


Ami*_*val 5

为方便读者,这里有一个处理多个设备响应的片段

public void sendMessageToMultipleDevices(String key, String value, ArrayList<String> devices) {

        Sender sender = new Sender(myApiKey);
        Message message = new Message.Builder().addData(key, value).build();
        try {
            MulticastResult result = sender.send(message, devices, 5);
            MTLog.info(TAG, "result " + result.toString());


            for (int i = 0; i < result.getTotal(); i++) {
                Result r = result.getResults().get(i);

                if (r.getMessageId() != null) {
                    String canonicalRegId = r.getCanonicalRegistrationId();
                    if (canonicalRegId != null) {
                        // devices.get(i) has more than on registration ID: update database

                    }
                } else {
                    String error = r.getErrorCodeName();
                    if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                        // application has been removed from devices.get(i) - unregister database
                    }
                }
            }
        } catch (IOException ex) {
            MTLog.err(TAG, "sending message failed", ex);
        }
    }
Run Code Online (Sandbox Code Playgroud)