猿问

如果最近发送过通知,如何防止再次发送?

我正在制作一个应用程序,它将 JSON 文档作为输入并以用户友好的方式显示信息。如果某些信息超出某些参数,该应用程序还会发送推送通知。我遇到的问题是信息需要非常最新,这意味着应用程序每 10 秒就会收到一个新的 JSON。这使得应用程序每 10 秒发送一次推送通知,这太频繁了。有没有办法让我指定一个休息时间,如果应用程序最近发送了通知,则不会发送通知?或者我可以这样做,如果用户没有清除通知,它就不会发送新通知?


总的来说,我对编程还比较陌生,对 Android-Studio 也很陌生。我在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);

我正在为我的业务制作这个应用程序,所以我不能在程序中留下任何公司特定的内容。如果有什么需要我澄清的,请告诉我!


我希望能够让它最快每小时只发送几次通知。理想情况下,也许每 30 分钟到一小时一次。


慕哥9229398
浏览 132回答 1
1回答

汪汪一只猫

如果您使用的是桌面设备,您可以查看 Google Guava,它有许多缓存实用程序,包括创建具有逐出时间的条目的功能。使用它,您可以添加条目并驱逐 10 分钟。然后,当新的 JSON 进来时,您可以检查它是否存在于缓存中。如果不是,则发送通知,如果是,则重新设置驱逐时间。您也可以编写自己的 EvictionMap。扩展ConcurrentHashMap,并在构造函数中创建一个线程并启动它。在线程内部,您可以检查 X 秒(听起来像是每 5 秒一次)并逐出条目。地图需要 <User, long>,其中 long 是驱逐时间。您可以创建自己的 put() 和 get() 以及可能的 touch() ,这会将驱逐时间重置为 System.getCurrentMillis();(我刚刚找到了几年前使用过的版本。它可以在管理线程的方式上进行一些改进)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){&nbsp; &nbsp; this.evictionTime = evictionTimeInSeconds * 1000;&nbsp; &nbsp; t = new EvictionThread(this, evictionTime);&nbsp; &nbsp; Thread thread = new Thread(t);&nbsp; &nbsp; thread.start();}public void touch(K o){&nbsp; &nbsp; evictionList.put(o, System.currentTimeMillis());}public void evict(){&nbsp; &nbsp; long current = System.currentTimeMillis();&nbsp; &nbsp; for (Iterator<K> i=evictionList.keySet().iterator(); i.hasNext();)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; K k = i.next();&nbsp; &nbsp; &nbsp; &nbsp; if (current > (evictionList.get(k) + evictionTime) )&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i.remove();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public void setEvictionTime(int timeInSeconds){&nbsp; &nbsp; evictionTime = timeInSeconds * 1000;&nbsp; &nbsp; t.setEvictionTime(evictionTime);}public Set<K> getKeys(){&nbsp; &nbsp; return evictionList.keySet();}public void stop(){&nbsp; &nbsp; t.shutDown();}@Overrideprotected void finalize(){&nbsp; &nbsp; t.shutDown();&nbsp; &nbsp;}private class EvictionThread implements Runnable{&nbsp; &nbsp; private volatile long evictionTime;&nbsp; &nbsp; private EvictionList list;&nbsp; &nbsp; private volatile boolean shouldRun = true;&nbsp; &nbsp; private EvictionThread(EvictionList list, long evictionTime)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.list = list;&nbsp; &nbsp; &nbsp; &nbsp; this.evictionTime = evictionTime;&nbsp; &nbsp; }&nbsp; &nbsp; public void shutDown()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; shouldRun = false;&nbsp; &nbsp; }&nbsp; &nbsp; public void setEvictionTime(long time)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; evictionTime = time;&nbsp; &nbsp; }&nbsp; &nbsp; public void run()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (shouldRun)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Thread.sleep(evictionTime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (Exception ex) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.evict();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}}
随时随地看视频慕课网APP

相关分类

Java
我要回答