Skip to content

Commit

Permalink
android notif: Accept a batch of message IDs for remove.
Browse files Browse the repository at this point in the history
This is the client logic to correspond with the server code in
zulip/zulip#11561 .

Existing servers only send the unbatched `zulip_message_id` field,
and perhaps servers in the future will send only the batched version
(e.g. after determining the client is new enough to understand it.)
For now, new servers will send a batch, plus the first ID redundantly
as `zulip_message_id`.  The semantics are clear for any combination --
just clear out any message appearing in either field -- so go ahead
and handle any combination.
  • Loading branch information
gnprice committed Feb 14, 2019
1 parent 3a34144 commit 45837d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

Expand All @@ -22,18 +23,11 @@

import java.io.IOException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;
import java.util.*;

import me.leolin.shortcutbadger.ShortcutBadger;

import static com.zulipmobile.notifications.NotificationHelper.buildNotificationContent;
import static com.zulipmobile.notifications.NotificationHelper.clearConversations;
import static com.zulipmobile.notifications.NotificationHelper.extractNames;
import static com.zulipmobile.notifications.NotificationHelper.extractTotalMessagesCount;
import static com.zulipmobile.notifications.NotificationHelper.addConversationToMap;
import static com.zulipmobile.notifications.NotificationHelper.removeMessageFromMap;
import static com.zulipmobile.notifications.NotificationHelper.TAG;
import static com.zulipmobile.notifications.NotificationHelper.*;

public class FCMPushNotifications {

Expand Down Expand Up @@ -75,8 +69,8 @@ static void onReceived(Context context, ConversationMap conversations, Map<Strin
updateNotification(context, conversations, props);
break;
case "remove":
final int zulipMessageId = Integer.parseInt(mapData.get("zulip_message_id"));
removeMessageFromMap(conversations, zulipMessageId);
final Set<Integer> messageIds = parseMessageIds(mapData);
removeMessagesFromMap(conversations, messageIds);
if (conversations.isEmpty()) {
getNotificationManager(context).cancelAll();
}
Expand All @@ -87,6 +81,23 @@ static void onReceived(Context context, ConversationMap conversations, Map<Strin
}
}

@NonNull
private static Set<Integer> parseMessageIds(Map<String, String> mapData) {
final Set<Integer> messageIds = new HashSet<>();
final String idStr = mapData.get("zulip_message_id");
if (idStr != null) {
messageIds.add(Integer.parseInt(idStr));
}
final String idsStr = mapData.get("zulip_message_ids");
if (idsStr != null) {
final String[] idStrs = idsStr.split(",");
for (final String idStr1 : idStrs) {
messageIds.add(Integer.parseInt(idStr1));
}
}
return messageIds;
}

private static void updateNotification(
Context context, ConversationMap conversations, PushNotificationsProp props) {
if (conversations.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,7 @@ public static void addConversationToMap(PushNotificationsProp prop, Conversation
conversations.put(key, messages);
}

public static void removeMessageFromMap(ConversationMap conversations, int zulipMessageId) {
final Set<Integer> messageIds = new HashSet<>(Collections.singletonList(zulipMessageId));
removeMessagesFromMap(conversations, messageIds);
}

private static void removeMessagesFromMap(ConversationMap conversations, Set<Integer> messageIds) {
static void removeMessagesFromMap(ConversationMap conversations, Set<Integer> messageIds) {
// We don't have the information to compute what key we ought to find each message under,
// so just walk the whole thing. If the user has >100 notifications, this linear scan
// won't be their worst problem anyway...
Expand Down

0 comments on commit 45837d8

Please sign in to comment.