2012-12-06 48 views
2

我正在创建一个使用Facebook和GCM的应用程序。用户可以在多个设备上使用该应用,朋友可以使用他的脸书ID向他发送GCM消息。GCM和id处理

在我的服务器上,我有一张桌子,它将facebook的id映射到gcm id。该表并不要求Facebook的ID是唯一的,所以消息可以发送到多个设备。

在文档,它演示了通过以下方式发送邮件时处理错误:

if (result.getMessageId() != null) { 
String canonicalRegId = result.getCanonicalRegistrationId(); 
if (canonicalRegId != null) { 
    //CASE 1 
    // same device has more than on registration ID: update database 
} 
} else { 
String error = result.getErrorCodeName(); 
if (error.equals(Constants.ERROR_NOT_REGISTERED)) { 
    // CASE 2 
    // application has been removed from device - unregister database 
} 
} 

CASE 1,它说与规范注册ID替换注册ID。但是,我怎么知道要替换的注册ID?假设接收器有2个设备,我有2个注册ID可供选择,但不知道我应该选择哪一个。

CASE 2中,也会出现同样的问题。我如何知道从数据库中删除哪个注册ID?

编辑
为清楚起见,这是我使用的时刻代码:

List<String> gcmIds = getGCMIds(c, fbId); 
if (gcmIds != null && !gcmIds.isEmpty()) { 
    Sender sender = new Sender(Params.GOOGLE_API_KEY); 
    Message message = new Message.Builder().addData("message", apiMessage.buildJSONString()).build(); 
    MulticastResult result = sender.send(message, gcmIds, 5); 

    List<Result> results = result.getResults(); 
     /* Do something with the results */ 

    success = result.getFailure() == 0; 
} else { 
    success = false; 
} 

回答

3

对于案例1:从docs

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.

发送邮件时到设备列表中,结果列表将以相同的顺序排列。因此,您实际上只需查找与结果中相同索引处使用的regID,然后替换dataStore中的registrationID即可。请记住保留规范ID,因为这是最近注册的。

对于情况2: 同样,删除处于同一索引处的regID。

要获得完整的示例实施,请查看官方的demo code,获取类似于您正在实施的发送呼叫。

+0

对于案例1:所以我仍然只有一个regid,不知道旧的regid在我​​的表中替换。 对于案例2,这似乎是一个体面的解决方案,谢谢! – nhaarman

+0

啊,道歉,更新我的回答澄清。基本上与案例2相同,您希望使用消息ID的本地记录以及将它们发送到哪个注册ID,并使用该记录执行查找:) –

+0

啊,当然。但现在的消息ID,我怎么才能得到它发送之前? Message类没有id getter。也许我应该不是一次发送消息,而是使用'send(Message,String,int)'而不是'send(Message,List ,int)'。然后我会知道我发送给它的regid,因为那是当时唯一的一个。看起来还是有点不好意思。 – nhaarman

2

你可能想看看GCM响应格式的文档:https://developer.android.com/google/gcm/gcm.html#response

特别是这部分:

结果

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 registration_id is set, replace the original ID with the new value (canonical ID) in your server database. Note that the original ID is not part of the result, so you need to obtain it from the list of registration_ids passed in the request (using the same index).

所以reg您需要更新的验证ID位于与您找到的规范ID相同的索引处(在为该消息创建的注册ID阵列中)。

+0

嗯,那是丑陋的..好吧,如果它是这样的,我不会抱怨。谢谢! – nhaarman