2017-03-07 64 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

不用担心队友:) –