From 1ab0492fcabacd7d911bc99ff8901cff5e12b2e2 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 13 Oct 2018 20:06:27 +0300 Subject: [PATCH] Suppress board notification when user is already subscribed to topic --- .../gr/thmmy/mthmmy/base/BaseActivity.java | 7 +++---- .../java/gr/thmmy/mthmmy/model/Bookmark.java | 12 ++++++++++++ .../mthmmy/services/NotificationService.java | 18 ++++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 3b2845ef..a8dc457e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -86,8 +86,8 @@ public abstract class BaseActivity extends AppCompatActivity { protected static SessionManager sessionManager; //Bookmarks - private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; - private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; + public static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; + public static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; protected Bookmark thisPageBookmark; private MenuItem thisPageBookmarkMenuButton; @@ -559,9 +559,8 @@ public abstract class BaseActivity extends AppCompatActivity { String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); if (tmpString != null) topicsBookmarked = Bookmark.arrayFromString(tmpString); - else { + else topicsBookmarked = new ArrayList<>(); - } tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); if (tmpString != null) diff --git a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java index 9e2aae95..1f383462 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java +++ b/app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java @@ -45,6 +45,18 @@ public class Bookmark implements java.io.Serializable { return false; } + public static boolean matchExistsById(ArrayList array, int id) { + if (array != null && !array.isEmpty()) { + for (Bookmark bookmark : array) { + if (bookmark != null) { + if (Objects.equals(Integer.parseInt(bookmark.getId()), id)) + return true; + } + } + } + return false; + } + public int findIndex(ArrayList array) { if (array != null && !array.isEmpty()) { for (int i = 0; i < array.size(); ++i) { diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java index 00dee3f3..d2d9f73c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java @@ -25,6 +25,7 @@ import org.json.JSONObject; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.model.Bookmark; import gr.thmmy.mthmmy.model.PostNotification; import timber.log.Timber; @@ -35,6 +36,9 @@ import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SELECTED_RING import static gr.thmmy.mthmmy.activities.settings.SettingsFragment.SETTINGS_SHARED_PREFS; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; +import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKED_TOPICS_KEY; +import static gr.thmmy.mthmmy.base.BaseActivity.BOOKMARKS_SHARED_PREFS; +import static gr.thmmy.mthmmy.model.Bookmark.matchExistsById; public class NotificationService extends FirebaseMessagingService { private static final int buildVersion = Build.VERSION.SDK_INT; @@ -52,15 +56,25 @@ public class NotificationService extends FirebaseMessagingService { if (Integer.parseInt(json.getString("posterId")) != userId) { int boardId = -1; String boardTitle = null; + int topicId = Integer.parseInt(json.getString("topicId")); if(remoteMessage.getFrom().contains("b")){ Timber.i("FCM BOARD type message detected."); - //TODO: return early and don't create notification if the user is also subscribed to this topicId + + SharedPreferences bookmarksFile = getSharedPreferences(BOOKMARKS_SHARED_PREFS, Context.MODE_PRIVATE); + String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); + if (tmpString != null){ + if(matchExistsById(Bookmark.arrayFromString(tmpString), topicId)){ + Timber.i("Board notification suppressed (already subscribed to topic)."); + return; + } + } + boardId = Integer.parseInt(json.getString("boardId")); boardTitle = json.getString("boardTitle"); } else Timber.i("FCM TOPIC type message detected."); - int topicId = Integer.parseInt(json.getString("topicId")); + int postId = Integer.parseInt(json.getString("postId")); String topicTitle = json.getString("topicTitle"); String poster = json.getString("poster");