編輯:關於 Android 程式設計
本文記錄了在學習 Android 推送時,服務器端如何透過「回執確認」保證一條推送真正送達的完整流程,並分析了示例代碼中的一個隱藏 Bug 及改進方案。
完整的推送應包含確認環節:服務器推送消息時生成一個唯一 uuid 標識該消息,並把消息與 uuid 存入數據庫;客戶端收到後取出 uuid 回傳給服務器;服務器收到 uuid 便到數據庫刪除對應記錄,整個推送才算完成。
public void sendNotifcationToUser(String apiKey, String username,
String title, String message, String uri) {
Random random = new Random();
// 這個 id 就是客戶端發送回執對應的 uuid
String id = Integer.toHexString(random.nextInt());
IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri);
ClientSession session = sessionManager.getSession(username);
if (session != null && session.getPresence().isAvailable()) {
notificationIQ.setTo(session.getAddress());
session.deliver(notificationIQ);
} else {
saveNotification(apiKey, username, title, message, uri, id);
}
// 不管用戶是否存在都入庫,直到收到回執後再刪除
User user = mUserService.getUserByUsername(username);
if (user != null) {
saveNotification(apiKey, username, title, message, uri, id);
}
}
源碼中還有這樣一段邏輯:當服務器檢測到客戶端由離線轉為上線時,會查數據庫是否有該用戶未送達的消息,有則取出發送:
List<Notification> list = mNotificationSevice.findNotificationByUsername(session.getUsername());
if (list != null && list.size() > 0) {
for (Notification notification : list) {
String apiKey = notification.getApiKey();
String title = notification.getTitle();
String message = notification.getMessage();
String uri = notification.getUri();
mNotificationManager.sendNotifcationToUser(apiKey, session.getUsername(), title, message, uri);
mNotificationSevice.deleteNotification(notification);
}
}
問題在於:檢測到有待發送消息時,先調用 sendNotifcationToUser(內部又入庫了一條新消息),接著 deleteNotification 把數據庫裡的記錄刪掉——這一行會把剛剛新入庫的那條也一起刪了。而新入庫的消息本應等客戶端回執後才刪除。
作者最初的做法是給方法加一個 shouldSave 參數,重發舊消息時傳 false 以跳過入庫。雖看似「沒有問題」,但實際上重發時根本沒入庫,客戶端回執後服務器也就無記錄可刪,只是「看起來」達到了效果,並不嚴謹。
更合理的做法是:重發舊消息時直接複用數據庫中已有的記錄——既不重新入庫,也不立即刪除,等收到回執後再刪。同時,重發完全沒必要生成新 uuid,直接取出原消息的 id 即可:
public void sendNotifcationToUser(String id, String apiKey, String username,
String title, String message, String uri, boolean shouldSave) {
IQ notificationIQ = createNotificationIQ(id, apiKey, title, message, uri);
ClientSession session = sessionManager.getSession(username);
if (session != null && session.getPresence().isAvailable()) {
notificationIQ.setTo(session.getAddress());
session.deliver(notificationIQ);
} else if (shouldSave) {
saveNotification(apiKey, username, title, message, uri, id);
}
User user = mUserService.getUserByUsername(username);
if (user != null && shouldSave) {
saveNotification(apiKey, username, title, message, uri, id);
}
}
重發舊消息時複用原 id、不再入庫:
List<Notification> list = mNotificationSevice.findNotificationByUsername(session.getUsername());
if (list != null && list.size() > 0) {
for (Notification notification : list) {
String apiKey = notification.getApiKey();
String title = notification.getTitle();
String message = notification.getMessage();
String uri = notification.getUri();
String id = notification.getUuid(); // 複用原消息的 uuid
mNotificationManager.sendNotifcationToUser(
id, apiKey, session.getUsername(), title, message, uri, false);
}
}
核心思路很簡單:重新發送時不再入庫,而是取出之前的消息來發送,等收到客戶端回執後再刪除,這樣既避免了重複,也保證了記錄始終與實際狀態一致。
Android IPC 之 AIDL 調用解析:從 Stub、Proxy 到系統 API 實戰
AIDL 是 Android 跨進程通信的重要方式之一。本文從 Service 中的 IBinder 出發,說明 Stub、Proxy、transact 與 onTran
App更新必备利器:CheckUpdateLibrary 库完整使用指南
檢查更新並下載新版本,幾乎可以說是每一款 App 都繞不開的核心能力。正因為它無處不在,背後要權衡的細節反而格外多,下面我們逐條拆解。 一、更新方式的兩種形態 當服務端上
Android 仿新浪微博 OAuth2.0 授權界面的完整實作教學(下篇)
OAuth2.0 授權流程看似繁瑣,但只要理清各環節的職責,就能輕鬆在 Android 中還原新浪微博的登入體驗。整體流程大致如下圖所示: 動手前的準備工作 首先前往新
在 App 內部切換語言且不影響系統語言設定
一、需求 有些應用需要在自身內部切換語言,又不能動到系統設定。例如系統是中文,App 切換成英文後,系統語言仍維持中文;反過來系統語言變了,App 也應該保持使用者先前挑