Browse Source

Disable notifications if own userID

pull/24/head
Ezerous 7 years ago
parent
commit
31c45128cb
  1. 19
      app/src/main/java/gr/thmmy/mthmmy/services/FirebaseService.java
  2. 30
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  3. 1
      app/src/main/res/layout/activity_bookmark_topic_row.xml

19
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.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.model.PostNotification; import gr.thmmy.mthmmy.model.PostNotification;
import gr.thmmy.mthmmy.session.SessionManager;
import timber.log.Timber; import timber.log.Timber;
import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH; import static android.support.v4.app.NotificationCompat.PRIORITY_HIGH;
@ -33,12 +35,16 @@ public class FirebaseService extends FirebaseMessagingService {
if (remoteMessage.getData().size() > 0) { if (remoteMessage.getData().size() > 0) {
JSONObject json = new JSONObject(remoteMessage.getData()); JSONObject json = new JSONObject(remoteMessage.getData());
try { try {
int topicId = Integer.parseInt(json.getString("topicId")); int userId = BaseApplication.getInstance().getSessionManager().getUserId();
int postId = Integer.parseInt(json.getString("postId")); //Don't notify me if the sender is me!
String topicTitle = json.getString("topicTitle"); if(Integer.parseInt(json.getString("posterId"))!= userId)
String poster = json.getString("poster"); {
sendNotification(new PostNotification(postId, topicId, topicTitle, poster)); 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) { } catch (JSONException e) {
Timber.e(e, "JSON Exception"); Timber.e(e, "JSON Exception");
} }
@ -85,6 +91,7 @@ public class FirebaseService extends FirebaseMessagingService {
.setSmallIcon(R.mipmap.ic_launcher) .setSmallIcon(R.mipmap.ic_launcher)
.setGroupSummary(true) .setGroupSummary(true)
.setGroup(GROUP_KEY) .setGroup(GROUP_KEY)
.setAutoCancel(true)
.setStyle(new NotificationCompat.InboxStyle() .setStyle(new NotificationCompat.InboxStyle()
.setSummaryText("New Posts")) .setSummaryText("New Posts"))
.setSound(defaultSoundUri) .setSound(defaultSoundUri)

30
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -57,6 +57,7 @@ public class SessionManager {
//Shared Preferences & its keys //Shared Preferences & its keys
private final SharedPreferences sharedPrefs; private final SharedPreferences sharedPrefs;
private static final String USERNAME = "Username"; private static final String USERNAME = "Username";
private static final String USER_ID = "UserID";
private static final String AVATAR_LINK = "AvatarLink"; private static final String AVATAR_LINK = "AvatarLink";
private static final String HAS_AVATAR = "HasAvatar"; private static final String HAS_AVATAR = "HasAvatar";
private static final String LOGOUT_LINK = "LogoutLink"; private static final String LOGOUT_LINK = "LogoutLink";
@ -118,6 +119,7 @@ public class SessionManager {
setLoginScreenAsDefault(false); setLoginScreenAsDefault(false);
sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply();
sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply();
sharedPrefs.edit().putInt(USER_ID, extractUserId(document)).apply();
String avatar = extractAvatarLink(document); String avatar = extractAvatarLink(document);
if (avatar != null) { if (avatar != null) {
sharedPrefs.edit().putBoolean(HAS_AVATAR, true).apply(); sharedPrefs.edit().putBoolean(HAS_AVATAR, true).apply();
@ -241,11 +243,15 @@ public class SessionManager {
//---------------------------------------GETTERS------------------------------------------------ //---------------------------------------GETTERS------------------------------------------------
public String getUsername() { 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() { public String getAvatarLink() {
return sharedPrefs.getString(AVATAR_LINK, "AvatarLink"); return sharedPrefs.getString(AVATAR_LINK, AVATAR_LINK);
} }
public boolean hasAvatar() { public boolean hasAvatar() {
@ -292,6 +298,7 @@ public class SessionManager {
cookieJar.clear(); cookieJar.clear();
sharedPrefs.edit().clear().apply(); //Clear session data sharedPrefs.edit().clear().apply(); //Clear session data
sharedPrefs.edit().putString(USERNAME, guestName).apply(); sharedPrefs.edit().putString(USERNAME, guestName).apply();
sharedPrefs.edit().putInt(USER_ID, -1).apply();
sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out
Timber.i("Session data cleared."); Timber.i("Session data cleared.");
} }
@ -333,6 +340,25 @@ public class SessionManager {
return "User"; //return a default username 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 @Nullable
private String extractAvatarLink(@NonNull Document doc) { private String extractAvatarLink(@NonNull Document doc) {

1
app/src/main/res/layout/activity_bookmark_topic_row.xml

@ -28,6 +28,7 @@
android:id="@+id/toggle_notification" android:id="@+id/toggle_notification"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:background="?android:attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/toggle_notification" android:contentDescription="@string/toggle_notification"
app:srcCompat="@drawable/ic_notification_on"/> app:srcCompat="@drawable/ic_notification_on"/>

Loading…
Cancel
Save