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

标签 java android android-studio notificationmanager

我正在制作一个应用程序,它将 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 分钟到一小时一次。

最佳答案

如果您使用的是桌面设备,则可以查看 Google Guava,它具有许多缓存实用程序,包括创建具有逐出时间的条目的功能。使用它,您可以添加条目并驱逐 10 分钟。然后,当新的 JSON 进来时,您可以检查它是否存在于缓存中。如果不是,则发送通知,如果是,则重新设置驱逐时间。

您也可以编写自己的 EvictionMap。扩展ConcurrentHashMap,并在构造函数中创建一个线程并启动它。在线程内部,您可以检查 X 秒(听起来像是每 5 秒一次)并逐出条目。 map 需要 ,其中 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)
{
    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();
        }
    }
}

}

关于java - 如果最近发送过通知,如何防止再次发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57811598/

相关文章:

android - 无法运行 android/sdk/build-tools/23.0.2/aapt

java - CMU Sphinx 5prealpha(语音识别系统)安装

java - 构造一个返回由输入决定的原始数据类型的 Java 方法?

android - 在 Android 中使用 Glide 加载图像

java - onUpgrade 数据库 - o​​ldVersion - newVersion

android - 如何修复 Android Studio 中找不到 Android 插件的问题

android - 远程调试AOSP框架服务

java - 从单个对象继承的不同类型的集合与单个 super 对象的集合

java - Sonarqube 使用 Cobertura 代替 Jacoco

android - 使用 AppCompat 将 Material 外观引入首选项的最佳实践?