2012-12-06 44 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