31

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
 }
}

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

    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 对象不包含注册 id,但如果第一个已过时,则仅包含规范 id。如果这两个列表是相互关联的(即保留顺序和大小),则没有记录。

我如何确定哪个结果是指哪个设备?

- 更新

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

4

3 回答 3

21

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

[id1, id4, id7, id8]

然后你得到的结果数组对于 id1、id4、id7 和 id8 将具有相同的顺序。

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

于 2012-07-21T17:49:04.983 回答
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);
        }
    }
于 2012-10-24T13:41:38.830 回答
3

此解决方案由谷歌开发人员示例GCM Demo 应用程序 注释asyncSend用于多播句柄完成

List<GcmUsers> devices=SearchRegisterdDevicesByCourseCommand.execute(instructorId, courseId);
    String status;
    if ( devices.equals(Collections.<GcmUsers>emptyList())) {    
      status = "Message ignored as there is no device registered!";
    } else {
      // NOTE: check below is for demonstration purposes; a real application
      // could always send a multicast, even for just one recipient
      if (devices.size() == 1) {
        // send a single message using plain post
        GcmUsers gcmUsers = devices.get(0);
        Message message = new Message.Builder().build();
        Result result = sender.send(message, gcmUsers.getGcmRegid(), 5);
        status = "Sent message to one device: " + result;
      } else {
        // send a multicast message using JSON
        // must split in chunks of 1000 devices (GCM limit)
        int total = devices.size();
        List<String> partialDevices = new ArrayList<String>(total);
        int counter = 0;
        int tasks = 0;
        for (GcmUsers device : devices) {
          counter++;
          partialDevices.add(device.getGcmRegid());
          int partialSize = partialDevices.size();
          if (partialSize == MULTICAST_SIZE || counter == total) {
            asyncSend(partialDevices);
            partialDevices.clear();
            tasks++;
          }
        }
        status = "Asynchronously sending " + tasks + " multicast messages to " +
            total + " devices";
      }
    }
    req.setAttribute(HomeServlet.ATTRIBUTE_STATUS, status.toString());






private void asyncSend(List<String> partialDevices) {
    // make a copy
    final List<String> devices = new ArrayList<String>(partialDevices);
    threadPool.execute(new Runnable() {

      public void run() {
        Message message = new Message.Builder().build();
        MulticastResult multicastResult;
        try {
          multicastResult = sender.send(message, devices, 5);
        } catch (IOException e) {
          logger.log(Level.SEVERE, "Error posting messages", e);
          return;
        }
        List<Result> results = multicastResult.getResults();
        // analyze the results
        for (int i = 0; i < devices.size(); i++) {
          String regId = devices.get(i);
          Result result = results.get(i);
          String messageId = result.getMessageId();
          if (messageId != null) {
            logger.fine("Succesfully sent message to device: " + regId +
                "; messageId = " + messageId);
            String canonicalRegId = result.getCanonicalRegistrationId();
            if (canonicalRegId != null) {
              // same device has more than on registration id: update it
              logger.info("canonicalRegId " + canonicalRegId);
              Datastore.updateRegistration(regId, canonicalRegId);
            }
          } else {
            String error = result.getErrorCodeName();
            if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
              // application has been removed from device - unregister it
              logger.info("Unregistered device: " + regId);
              Datastore.unregister(regId);
            } else {
              logger.severe("Error sending message to " + regId + ": " + error);
            }
          }
        }
      }});
  }
于 2013-06-06T17:41:45.760 回答