我正在构建一个应用程序,该应用程序每30分钟都有每30分钟的服务,以要求客户拥有的其余单元。在检查期间,如果客户端的单位低于指定的单位,则该应用将发送一份本地通知,即单位运行较低。一切都很好。
我遇到的问题是,如果说客户端有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")
}
}
})
您应该使用标志识别通知是否在启动应用程序开始时发送或不发送通知,除了 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
;