亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果最近發送過通知,如何防止再次發送?

如果最近發送過通知,如何防止再次發送?

慕哥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 關注
  • 162 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號