[英]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. 我正在制作一个将JSON文档作为输入并以用户友好的方式显示信息的应用程序。 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.
我遇到的问题是信息需要非常最新,这意味着应用程序每10秒就会收到一个新的JSON。 That makes the app send a push notification every 10 seconds, which is way too often.
这样一来,该应用就会每10秒发送一次推送通知,这种情况经常发生。 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. 总的来说,我对编程不太熟悉,对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.
我在Android开发人员页面上查看了NotificationManager,看那里是否有东西,但找不到任何东西。
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.
理想情况下,也许每30分钟到一个小时一次。
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. 如果您使用的是台式机,则可以看看Google Guava,它具有许多缓存实用程序,包括能够创建具有驱逐时间的条目的功能。 Using that, you could add entries with an eviction of 10 minutes.
使用此功能,您可以添加驱逐时间为10分钟的条目。 Then, when a new JSON comes in, you can check if it exists in the cache.
然后,当新的JSON传入时,您可以检查它是否存在于缓存中。 If no, send the notification, if yes, reset the eviction time for it.
如果否,则发送通知,如果是,则重置其驱逐时间。
You could also write your own EvictionMap. 您也可以编写自己的EvictionMap。 Extend ConcurrentHashMap, and in the constructor create a thread and start it.
扩展ConcurrentHashMap,并在构造函数中创建一个线程并启动它。 Inside the thread, you can check X seconds (sounds like every 5 seconds for you) and evict entries.
在线程内部,您可以检查X秒(听起来像您每5秒一次的声音)并逐出条目。 The Map would require <User, long> where the long is the eviction time.
该地图需要<User,long>,其中long是驱逐时间。 You can create your own put() and get() and maybe a touch() which would reset the eviction time to System.getCurrentMillis();
您可以创建自己的put()和get(),还可以创建touch(),这会将收回时间重置为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();
}
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.