Checks to see if notification is displayed correctly before showing notification - avoids multiple notification sounds etc. Fixes #98

This commit is contained in:
Graham Jones
2023-06-11 20:53:15 +01:00
parent c527a5f5ca
commit 78b9f72829

View File

@@ -54,6 +54,7 @@ import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.os.PowerManager.WakeLock; import android.os.PowerManager.WakeLock;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.service.notification.StatusBarNotification;
import android.telephony.SmsManager; import android.telephony.SmsManager;
import android.text.format.Time; import android.text.format.Time;
import android.util.Log; import android.util.Log;
@@ -96,6 +97,7 @@ public class SdServer extends Service implements SdDataReceiver {
private NotificationManager mNM; private NotificationManager mNM;
private NotificationCompat.Builder mNotificationBuilder; private NotificationCompat.Builder mNotificationBuilder;
private Notification mNotification; private Notification mNotification;
private int mCurrentNotificationAlarmLevel = -999;
private SdWebServer webServer = null; private SdWebServer webServer = null;
private final static String TAG = "SdServer"; private final static String TAG = "SdServer";
private Timer dataLogTimer = null; private Timer dataLogTimer = null;
@@ -465,10 +467,16 @@ public class SdServer extends Service implements SdDataReceiver {
* Show a notification while this service is running. * Show a notification while this service is running.
*/ */
private void showNotification(int alarmLevel) { private void showNotification(int alarmLevel) {
Log.v(TAG, "showNotification() - alarmLevel=" + alarmLevel);
int iconId; int iconId;
String titleStr; String titleStr;
Uri soundUri = null; Uri soundUri = null;
if ((alarmLevel == mCurrentNotificationAlarmLevel) && (isNotificationShown(NOTIFICATION_ID))) {
Log.v(TAG,"showNotification - notification already shown at specified alarm level - not doing anything");
return;
}
Log.v(TAG, "showNotification() - alarmLevel=" + alarmLevel);
switch (alarmLevel) { switch (alarmLevel) {
case 0: case 0:
iconId = R.drawable.star_of_life_24x24; iconId = R.drawable.star_of_life_24x24;
@@ -534,6 +542,7 @@ public class SdServer extends Service implements SdDataReceiver {
} }
} }
mNM.notify(NOTIFICATION_ID, mNotification); mNM.notify(NOTIFICATION_ID, mNotification);
mCurrentNotificationAlarmLevel = alarmLevel;
} else { } else {
Log.i(TAG, "showNotification() - notification builder is null, so not showing notification."); Log.i(TAG, "showNotification() - notification builder is null, so not showing notification.");
} }
@@ -1650,6 +1659,11 @@ public class SdServer extends Service implements SdDataReceiver {
String titleStr; String titleStr;
Uri soundUri = null; Uri soundUri = null;
if (isNotificationShown(EVENT_NOTIFICATION_ID)) {
Log.v(TAG,"showEventNotification() - notification is already shown, so not doing anything");
return;
}
// Initialise Notification channel for API level 26 and over // Initialise Notification channel for API level 26 and over
// from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26 // from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
@@ -1694,6 +1708,11 @@ public class SdServer extends Service implements SdDataReceiver {
String titleStr; String titleStr;
Uri soundUri = null; Uri soundUri = null;
if (isNotificationShown(DATASHARE_NOTIFICATION_ID)) {
Log.v(TAG,"showDataShareNotification() - notification is already shown, so not doing anything");
return;
}
// Initialise Notification channel for API level 26 and over // Initialise Notification channel for API level 26 and over
// from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26 // from https://stackoverflow.com/questions/44443690/notificationcompat-with-api-26
NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nM = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
@@ -1737,7 +1756,21 @@ public class SdServer extends Service implements SdDataReceiver {
nM.notify(DATASHARE_NOTIFICATION_ID, notification); nM.notify(DATASHARE_NOTIFICATION_ID, notification);
} }
/**
* isNotificationShown - returns true if the specified notificationID is shown, otherwise returns false.
* @param notificationId - Notification ID to check
* @return true if the specified notification is displayed, otherwise false.
*/
private boolean isNotificationShown(int notificationId) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
if (notification.getId() == notificationId) {
return(true);
}
}
return(false);
}
} }