diff --git a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java b/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java index cc58c02a..b6fcda79 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java +++ b/app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java @@ -19,7 +19,9 @@ 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.PostNotification; +import gr.thmmy.mthmmy.session.SessionManager; import timber.log.Timber; import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH; @@ -33,12 +35,16 @@ public class FirebaseService extends FirebaseMessagingService { if (remoteMessage.getData().size() > 0) { JSONObject json = new JSONObject(remoteMessage.getData()); try { - int topicId = Integer.parseInt(json.getString("topicId")); - int postId = Integer.parseInt(json.getString("postId")); - String topicTitle = json.getString("topicTitle"); - String poster = json.getString("poster"); - sendNotification(new PostNotification(postId, topicId, topicTitle, poster)); - + int userId = BaseApplication.getInstance().getSessionManager().getUserId(); + //Don't notify me if the sender is me! + if(Integer.parseInt(json.getString("posterId"))!= userId) + { + int topicId = Integer.parseInt(json.getString("topicId")); + int postId = Integer.parseInt(json.getString("postId")); + String topicTitle = json.getString("topicTitle"); + String poster = json.getString("poster"); + sendNotification(new PostNotification(postId, topicId, topicTitle, poster)); + } } catch (JSONException e) { Timber.e(e, "JSON Exception"); } @@ -85,6 +91,7 @@ public class FirebaseService extends FirebaseMessagingService { .setSmallIcon(R.mipmap.ic_launcher) .setGroupSummary(true) .setGroup(GROUP_KEY) + .setAutoCancel(true) .setStyle(new NotificationCompat.InboxStyle() .setSummaryText("New Posts")) .setSound(defaultSoundUri) diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 8d534293..7f976aaa 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -57,6 +57,7 @@ public class SessionManager { //Shared Preferences & its keys private final SharedPreferences sharedPrefs; private static final String USERNAME = "Username"; + private static final String USER_ID = "UserID"; private static final String AVATAR_LINK = "AvatarLink"; private static final String HAS_AVATAR = "HasAvatar"; private static final String LOGOUT_LINK = "LogoutLink"; @@ -118,6 +119,7 @@ public class SessionManager { setLoginScreenAsDefault(false); sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); + sharedPrefs.edit().putInt(USER_ID, extractUserId(document)).apply(); String avatar = extractAvatarLink(document); if (avatar != null) { sharedPrefs.edit().putBoolean(HAS_AVATAR, true).apply(); @@ -241,11 +243,15 @@ public class SessionManager { //---------------------------------------GETTERS------------------------------------------------ public String getUsername() { - return sharedPrefs.getString(USERNAME, "Username"); + return sharedPrefs.getString(USERNAME, USERNAME); + } + + public int getUserId() { + return sharedPrefs.getInt(USER_ID, -1); } public String getAvatarLink() { - return sharedPrefs.getString(AVATAR_LINK, "AvatarLink"); + return sharedPrefs.getString(AVATAR_LINK, AVATAR_LINK); } public boolean hasAvatar() { @@ -292,6 +298,7 @@ public class SessionManager { cookieJar.clear(); sharedPrefs.edit().clear().apply(); //Clear session data sharedPrefs.edit().putString(USERNAME, guestName).apply(); + sharedPrefs.edit().putInt(USER_ID, -1).apply(); sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out Timber.i("Session data cleared."); } @@ -333,6 +340,25 @@ public class SessionManager { return "User"; //return a default username } + @NonNull + private int extractUserId(@NonNull Document doc) { + try{ + Elements elements = doc.select("a:containsOwn(Εμφάνιση των μηνυμάτων σας), a:containsOwn(Show own posts)"); + if (elements.size() == 1) { + String link = elements.first().attr("href"); + + Pattern pattern = Pattern.compile("https://www.thmmy.gr/smf/index.php\\?action=profile;u=(\\d*);sa=showPosts"); + Matcher matcher = pattern.matcher(link); + if (matcher.find()) + return Integer.parseInt(matcher.group(1)); + } + } catch (Exception e) { + Timber.e(new ParseException("Parsing failed(user id extraction)"),"ParseException"); + } + Timber.e(new ParseException("Parsing failed(user id extraction)"),"ParseException"); + return -1; + } + @Nullable private String extractAvatarLink(@NonNull Document doc) { diff --git a/app/src/main/res/layout/activity_bookmark_topic_row.xml b/app/src/main/res/layout/activity_bookmark_topic_row.xml index c5b54d6f..09158cb6 100644 --- a/app/src/main/res/layout/activity_bookmark_topic_row.xml +++ b/app/src/main/res/layout/activity_bookmark_topic_row.xml @@ -28,6 +28,7 @@ android:id="@+id/toggle_notification" android:layout_width="wrap_content" android:layout_height="wrap_content" + android:layout_marginEnd="12dp" android:background="?android:attr/selectableItemBackground" android:contentDescription="@string/toggle_notification" app:srcCompat="@drawable/ic_notification_on"/>