Browse Source

Suppress board notification when user is already subscribed to topic

pull/61/merge
Ezerous 6 years ago
parent
commit
1ab0492fca
No known key found for this signature in database GPG Key ID: 262B2954BBA319E3
  1. 7
      app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java
  2. 12
      app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java
  3. 18
      app/src/main/java/gr/thmmy/mthmmy/services/NotificationService.java

7
app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java

@ -86,8 +86,8 @@ public abstract class BaseActivity extends AppCompatActivity {
protected static SessionManager sessionManager; protected static SessionManager sessionManager;
//Bookmarks //Bookmarks
private static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs"; public static final String BOOKMARKS_SHARED_PREFS = "bookmarksSharedPrefs";
private static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey"; public static final String BOOKMARKED_TOPICS_KEY = "bookmarkedTopicsKey";
private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey"; private static final String BOOKMARKED_BOARDS_KEY = "bookmarkedBoardsKey";
protected Bookmark thisPageBookmark; protected Bookmark thisPageBookmark;
private MenuItem thisPageBookmarkMenuButton; private MenuItem thisPageBookmarkMenuButton;
@ -559,9 +559,8 @@ public abstract class BaseActivity extends AppCompatActivity {
String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null); String tmpString = bookmarksFile.getString(BOOKMARKED_TOPICS_KEY, null);
if (tmpString != null) if (tmpString != null)
topicsBookmarked = Bookmark.arrayFromString(tmpString); topicsBookmarked = Bookmark.arrayFromString(tmpString);
else { else
topicsBookmarked = new ArrayList<>(); topicsBookmarked = new ArrayList<>();
}
tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null); tmpString = bookmarksFile.getString(BOOKMARKED_BOARDS_KEY, null);
if (tmpString != null) if (tmpString != null)

12
app/src/main/java/gr/thmmy/mthmmy/model/Bookmark.java

@ -45,6 +45,18 @@ public class Bookmark implements java.io.Serializable {
return false; return false;
} }
public static boolean matchExistsById(ArrayList<Bookmark> 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<Bookmark> array) { public int findIndex(ArrayList<Bookmark> array) {
if (array != null && !array.isEmpty()) { if (array != null && !array.isEmpty()) {
for (int i = 0; i < array.size(); ++i) { for (int i = 0; i < array.size(); ++i) {

18
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.R;
import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.activities.topic.TopicActivity;
import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseApplication;
import gr.thmmy.mthmmy.model.Bookmark;
import gr.thmmy.mthmmy.model.PostNotification; import gr.thmmy.mthmmy.model.PostNotification;
import timber.log.Timber; 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.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_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.BUNDLE_TOPIC_URL; 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 { public class NotificationService extends FirebaseMessagingService {
private static final int buildVersion = Build.VERSION.SDK_INT; 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) { if (Integer.parseInt(json.getString("posterId")) != userId) {
int boardId = -1; int boardId = -1;
String boardTitle = null; String boardTitle = null;
int topicId = Integer.parseInt(json.getString("topicId"));
if(remoteMessage.getFrom().contains("b")){ if(remoteMessage.getFrom().contains("b")){
Timber.i("FCM BOARD type message detected."); 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")); boardId = Integer.parseInt(json.getString("boardId"));
boardTitle = json.getString("boardTitle"); boardTitle = json.getString("boardTitle");
} }
else else
Timber.i("FCM TOPIC type message detected."); Timber.i("FCM TOPIC type message detected.");
int topicId = Integer.parseInt(json.getString("topicId"));
int postId = Integer.parseInt(json.getString("postId")); int postId = Integer.parseInt(json.getString("postId"));
String topicTitle = json.getString("topicTitle"); String topicTitle = json.getString("topicTitle");
String poster = json.getString("poster"); String poster = json.getString("poster");

Loading…
Cancel
Save