title: GCM: MulticastResult - which result is from which device?
body: <p>Following the last section in the <a href="http://developer.android.com/guide/google/gcm/gs.html#server-app" rel="noreferrer">GCM: Getting Started</a> guide, there's some book-keeping to be done after receiving the results.</p>
  
  <p>Quoting from the guide:</p>
  
  <blockquote>
    <p>It's now necessary to parse the result and take the proper action in the following cases:</p>
    
    <ul>
    <li>If the message was created but the result returned a canonical    registration ID, it's necessary to replace the current registration<br>
    ID with the canonical one.</li>
    <li>If the returned error is NotRegistered, it's necessary to remove that    registration ID, because the application was uninstalled from
    the    device.</li>
    </ul>
    
    <p>Here's a code snippet that handles these 2 conditions:</p>
  
  <pre><code>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
   }
  }
  </code></pre>
  </blockquote>
  
  <p>The guide above refers to a <strong>single</strong> result, and not to the multicast case.
  I'm not sure how to handle the multicast case:</p>
  
  <pre><code>    ArrayList&lt;String&gt; devices = new ArrayList&lt;String&gt;();
  
      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);
      }
  </code></pre>
  
  <p>I submit a list of devices, and receive back a list of results.
  The Result object doesn't contain the registration id, but only a canonical id if the first is obsolete.
  It is undocumented if the two lists are co-related (ie. preserves order and size).</p>
  
  <p>How can I be sure which result refer to which device?</p>
  
  <p><strong>-- UPDATE</strong></p>
  
  <p><strong>I've pasted a snippet of the solution in a separate answer below</strong></p>