简体   繁体   中英

Is there a way to prevent my app from sending notifications for a certain time frame?

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.

As carlosgub mentioned, you can use preferences hard-coded by yourself or entered by the user to determine when to send them. Here is some sample code:

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Date;

public class YourClass {

    //Key in Shared Preferences to use
    private static final String LAST_UPDATE = "lastUpdate";
    //10 minutes, adjust to your preferences
    private static final long NUMBER_MILLISECONDS_BETWEEN_UPDATES = (1000L * 60L * 10L);
    //Your Shared Preferences name
    private static final String SP_NAME = "yourSPName";

    private static void saveLastUpdateTime(Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        long x = new Date().getTime();
        editor.putLong(LAST_UPDATE, x);
        editor.commit();
    }

    private static long getLastUpdateTime(Context context) {
        SharedPreferences sharedPref = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        return sharedPref.getLong(LAST_UPDATE, -1);
    }

    public static boolean shouldSendNotification(Context context) {
        long lastUpdate = getLastUpdateTime(context);
        if (lastUpdate == -1) {
            saveLastUpdateTime(context);
            //This means it has yet to run
            return true;
        }
        long now = new Date().getTime();
        if ((now - lastUpdate) > NUMBER_MILLISECONDS_BETWEEN_UPDATES) {
            saveLastUpdateTime(context);
            return true;
        } else {
            return false;
        }
    }
}

In the code here, you call shouldSendNotification() and if it returns false, it has yet to be 10 minutes since the last time it ran, if it returns true, it has been over 10 minutes since the last time it ran (or has never run at all).

To use this with your code, at the top of the method that is building the notify object, just call it and if it returns false, return out:

if(!YourClass.shouldSendNotification(context)){
    return;
}

Just make sure to adjust the values up top to your preferences (Pun intended).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM