简体   繁体   中英

How can I prevent a notification from sending again if it was recently sent?

I am making an app that takes a JSON document as input and displays the information in a user-friendly way. The app will also send a push notification if certain information is outside certain parameters. The problem I am running into is that the information needs to be very up-to-date, this means the app receives a new JSON every 10 seconds. That makes the app send a push notification every 10 seconds, which is way too often. Is there a way for me to either specify a break period where the app will not send a notification if it has recently sent one? Or could I make it so if the user doesn't clear the notification, it doesn't send a new one?

I am relatively new to programming in general, and really new to Android-Studio. I have looked on the Android Developers page for NotificationManager to see if there was something there, but I was unable to find anything.

    if variable1") < variable1MinValue || variable1 > variable1MaxValue|| 
    variable2 < variable2MinValue|| variable2 > variable2MaxValue){
                                                NotificationManager notif= 
    (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify=new Notification.Builder

    (getApplicationContext()).setContentTitle("ERROR: value 
    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 am making this app for my business, so I can't leave anything company specific in the program. If there is anything I need to clarify, please let me know!

I am hoping to be able to get it to only send the notification a few times every hour at the fastest. Ideally, maybe once every 30 minutes to an hour.

If you're on desktop, you could look at Google Guava, which has many caching utilities, including the ability to create entries with eviction times. Using that, you could add entries with an eviction of 10 minutes. Then, when a new JSON comes in, you can check if it exists in the cache. If no, send the notification, if yes, reset the eviction time for it.

You could also write your own EvictionMap. Extend ConcurrentHashMap, and in the constructor create a thread and start it. Inside the thread, you can check X seconds (sounds like every 5 seconds for you) and evict entries. The Map would require <User, long> where the long is the eviction time. You can create your own put() and get() and maybe a touch() which would reset the eviction time to System.getCurrentMillis();

(I just found a version I had used years ago. It could use some improvement with how it manages the Thread)

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class EvictionList<K>

{

private final ConcurrentHashMap<K, Long> evictionList = new ConcurrentHashMap<K, Long>();
private long evictionTime;
private final EvictionThread t;
public EvictionList(int evictionTimeInSeconds)
{
    this.evictionTime = evictionTimeInSeconds * 1000;
    t = new EvictionThread(this, evictionTime);
    Thread thread = new Thread(t);
    thread.start();
}

public void touch(K o)
{
    evictionList.put(o, System.currentTimeMillis());
}

public void evict()
{
    long current = System.currentTimeMillis();
    for (Iterator<K> i=evictionList.keySet().iterator(); i.hasNext();)
    {
        K k = i.next();
        if (current > (evictionList.get(k) + evictionTime) )
        {
            i.remove();
        }
    }
}

public void setEvictionTime(int timeInSeconds)
{
    evictionTime = timeInSeconds * 1000;
    t.setEvictionTime(evictionTime);
}

public Set<K> getKeys()
{
    return evictionList.keySet();
}

public void stop()
{
    t.shutDown();
}

@Override
protected void finalize()
{
    t.shutDown();   
}

private class EvictionThread implements Runnable
{
    private volatile long evictionTime;
    private EvictionList list;
    private volatile boolean shouldRun = true;

    private EvictionThread(EvictionList list, long evictionTime)
    {
        this.list = list;
        this.evictionTime = evictionTime;
    }

    public void shutDown()
    {
        shouldRun = false;
    }

    public void setEvictionTime(long time)
    {
        evictionTime = time;
    }

    public void run()
    {
        while (shouldRun)
        {
            try
            {
                Thread.sleep(evictionTime);
            }
            catch (Exception ex) {}
            list.evict();
        }
    }
}

}

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