Only show the new messages within the the notification interval to prevent showing the same notifications over and over again. Version 1.1.1.

This commit is contained in:
Alex Ning 2019-09-22 22:02:50 +08:00
parent 52b27971ff
commit 41b0b31f2c
4 changed files with 31 additions and 13 deletions

Binary file not shown.

Binary file not shown.

View File

@ -6,8 +6,8 @@ android {
applicationId "ml.docilealligator.infinityforreddit" applicationId "ml.docilealligator.infinityforreddit"
minSdkVersion 21 minSdkVersion 21
targetSdkVersion 29 targetSdkVersion 29
versionCode 11 versionCode 12
versionName "1.1.0" versionName "1.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
buildTypes { buildTypes {

View File

@ -3,7 +3,9 @@ package ml.docilealligator.infinityforreddit;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
@ -16,6 +18,7 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -42,7 +45,10 @@ public class PullNotificationWorker extends Worker {
Retrofit mRetrofit; Retrofit mRetrofit;
@Inject @Inject
RedditDataRoomDatabase redditDataRoomDatabase; RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
SharedPreferences mSharedPreferences;
public PullNotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { public PullNotificationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams); super(context, workerParams);
@ -54,7 +60,7 @@ public class PullNotificationWorker extends Worker {
@Override @Override
public Result doWork() { public Result doWork() {
try { try {
List<Account> accounts = redditDataRoomDatabase.accountDao().getAllAccounts(); List<Account> accounts = mRedditDataRoomDatabase.accountDao().getAllAccounts();
for(int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) { for(int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) {
Account account = accounts.get(accountIndex); Account account = accounts.get(accountIndex);
@ -78,9 +84,19 @@ public class PullNotificationWorker extends Worker {
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle(); NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
int messageSize = messages.size() >= 5 ? 5 : messages.size(); int messageSize = messages.size() >= 5 ? 5 : messages.size();
long currentTime = Calendar.getInstance().getTimeInMillis();
long notificationInterval = Long.parseLong(
mSharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"))
* 1000 * 60 * 60;
boolean hasValidMessage = false;
for(int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) { for(int messageIndex = messageSize - 1; messageIndex >= 0; messageIndex--) {
Message message = messages.get(messageIndex); Message message = messages.get(messageIndex);
if(currentTime - message.getTimeUTC() > notificationInterval) {
continue;
}
hasValidMessage = true;
inboxStyle.addLine(message.getAuthor() + " " + message.getBody()); inboxStyle.addLine(message.getAuthor() + " " + message.getBody());
@ -149,6 +165,7 @@ public class PullNotificationWorker extends Worker {
notificationManager.notify(NotificationUtils.getNotificationIdUnreadMessage(accountIndex, messageIndex), builder.build()); notificationManager.notify(NotificationUtils.getNotificationIdUnreadMessage(accountIndex, messageIndex), builder.build());
} }
if(hasValidMessage) {
inboxStyle.setBigContentTitle(context.getString(R.string.notification_new_messages, messages.size())) inboxStyle.setBigContentTitle(context.getString(R.string.notification_new_messages, messages.size()))
.setSummaryText(accountName); .setSummaryText(accountName);
@ -160,6 +177,7 @@ public class PullNotificationWorker extends Worker {
summaryBuilder.setContentIntent(summaryPendingIntent); summaryBuilder.setContentIntent(summaryPendingIntent);
notificationManager.notify(NotificationUtils.getSummaryIdUnreadMessage(accountIndex), summaryBuilder.build()); notificationManager.notify(NotificationUtils.getSummaryIdUnreadMessage(accountIndex), summaryBuilder.build());
}
} else { } else {
return Result.success(); return Result.success();
} }
@ -218,7 +236,7 @@ public class PullNotificationWorker extends Worker {
if(response.isSuccessful() && response.body() != null) { if(response.isSuccessful() && response.body() != null) {
JSONObject jsonObject = new JSONObject(response.body().toString()); JSONObject jsonObject = new JSONObject(response.body().toString());
String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY); String newAccessToken = jsonObject.getString(RedditUtils.ACCESS_TOKEN_KEY);
redditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken); mRedditDataRoomDatabase.accountDao().changeAccessToken(account.getUsername(), newAccessToken);
return newAccessToken; return newAccessToken;
} }
return ""; return "";