为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕哥9229398 2023-07-13 16:57:57
我正在制作一个应用程序,它将 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 分钟到一小时一次。
查看完整描述

1 回答

?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

如果您使用的是桌面设备,您可以查看 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)

{

    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();

        }

    }

}

}


查看完整回答
反对 回复 2023-07-13
  • 1 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信