2017-03-07 45 views
0

我正在構建一個應用程序,該應用程序每30分鐘呼叫一次服務請求客戶端擁有的其餘單元的值。在檢查期間,如果客戶端的單位低於指定的單位,應用程序將發送一個本地通知,表明這些單位運行不足。一切工作正常。Ionic Angular運行計劃通知一次

我遇到的問題是,如果我們說客戶有90個單位,限制爲100,應用程序將發送通知罰款,但30分鐘後,當它再次檢查,看到客戶有85個單位,它會一次又一次地發送,直到客戶端充電。我想要的是通知發送一次。任何幫助將不勝感激。以下是我的代碼。

this.storage.get('AlertOn100').then((val) => { 
     if (val==true){ 
     if(res.power<100 && res.power>50){ 
      LocalNotifications.schedule({ 
      title: "Electricity is below 100 Units", 
      text: "Please recharge to avoid running out", 
      at: new Date(new Date().getTime() + 1 * 60 * 1000), 
      sound: null 
      }); 
       console.log("Units Below 100 . The guy should be notified") 
     }else{ 
      console.log("Don't send notification at 100 Units because the units are above that value") 
     } 
     } 
     }) 

回答

0

你應該用一個標誌來識別通知是否發送,當應用程序被啓動節約鑰匙插入localstorage與名稱flag和值將是false當通知發送到客戶端成功,您可以更改標記爲true,下次如果它是真的,則不發送通知。這將是爲你工作與你的存儲類型更換的localStorage等

this.storage.get('AlertOn100').then((val) => { 
    var flag = localStorage.get("flag"); 
    if (val==true && !flag){ 
    if(res.power<100 && res.power>50){ 
     LocalNotifications.schedule({ 
     title: "Electricity is below 100 Units", 
     text: "Please recharge to avoid running out", 
     at: new Date(new Date().getTime() + 1 * 60 * 1000), 
     sound: null 
     }); 
     localStorage.set("flag", true); 
      console.log("Units Below 100 . The guy should be notified") 
    }else{ 
     console.log("Don't send notification at 100 Units because the units are above that value") 
    } 
    } 
    }) 

,當用戶充值設置標記,以false;

+0

嘿,非常感謝。有用 。並抱歉遲到回覆。 – bobin56

+0

不用擔心隊友:) –