I am trying to make an app that will notify the user when data the app receives is not what it should be (think steam being below 212 degrees Fahrenheit). The app also shows the information if the user is in the app. I was able to get the notification to send whenever this was the case. The only problem is that the information needs to be really up-to-date, so it is updated every 10 seconds. This makes the app send a notification every 10 seconds if the data is continuously incorrect. Is there a way to prevent recurring notifications for a specified time? (Around 10 - 15 minutes)
I have tried using thread.sleep(1000)
inside a for
loop to make it wait the 10 minutes, but that pauses the entire updating system for 10 minutes, so no information is going to the app. I am new to android studio and couldn't find anything online to help me with this.
This is just the way the app knows to send a notification. If I could continue using this, that would be ideal, but if there is a better way, I am open to changing it.
//ERROR Notification
if (map.get("steamTemp") < 212 || map.get("steamTemp") > 500 ||
map.get("waterTemp") < 40 || map.get("waterTemp") > 150||
map.get("dieselFlow") < 50 || map.get("dieselFlow") > 100 ||
map.get("waterFlow") < 50 || map.get("waterFlow") > 100||
map.get("waterFeederLevel") < 10 || map.get("waterFeederLevel") > 150) {
NotificationManager notif = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification.Builder
(getApplicationContext())
.setContentTitle("ERROR: Device Error")
.setContentText("Please see app for more information.")
.setSmallIcon(R.drawable.error_notif)
.setSound(soundUri)
.build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
I would like a notification to be sent the moment something is wrong, but after that to wait 10 minutes before sending another one. The problem with the thread.sleep(1000)
example I explained above is that it paused the entire app, not just the notification. This is not okay as the app needs to show updated information if the user looks at it.