Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 推送進階:服務器端「回執確認」機制實踐與 Bug 修復筆記

編輯:關於 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);
    }
}

示例代碼中的 Bug

問題在於:檢測到有待發送消息時,先調用 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);
    }
}

核心思路很簡單:重新發送時不再入庫,而是取出之前的消息來發送,等收到客戶端回執後再刪除,這樣既避免了重複,也保證了記錄始終與實際狀態一致。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved