需要实现像功能一样的看门狗到服务器更新。离子
问题描述:
目前我正在使用下面的代码来检查任何服务器更新,检查服务器在每5秒,如果从服务器更新显示通知和弹出。但我不想用户定时器,而是想要像新的短信或电话来的东西来。同样,服务器上的任何更新都会在不需要人们观察的情况下花费太多时间。需要实现像功能一样的看门狗到服务器更新。离子
setInterval(function(){
this.dbProcess.dataProcess('','getServerUpdate.php').subscribe(
data => {
//console.log("getRequests", data);
this.response = data;
this.response = this.response._body;
this.response = JSON.parse(this.response);
console.log(this.response.REQUEST)
console.log("Timer Running : " + localStorage.getItem('timer'))
if(this.response.RESPONSE == "success"){
console.log("success");
this.timer = localStorage.getItem('timer');
clearInterval(this.timer);
localStorage.setItem('timer', "end");
console.log("Timer Ended" + this.timer);
this.vibration.vibrate(1000);
this.localNotifications.schedule({
id: this.response.REQUEST.RequestId,
title: 'New Ride Request (' + this.response.REQUEST.firstname + ' ' +this.response.REQUEST.lastname + ')' ,
text: this.response.REQUEST.FromLocation + ' ' +this.response.REQUEST.ToLocation + ' ' + this.response.REQUEST.EstimatedCost,
}
);
this.modal = this.modalCtrl.create(JobRequestPage);
this.modal.present();
}
else{
console.log("Failure");
}
},
err => { //on error this will execute
console.log("server error");
//this.showError(err);
},
() =>{ // on complete this will execute
console.log("completed");
}
);
}, 5000);
答
您正在寻找推送通知。
您应该通过Firebase,Onesignal,Amazon或其他人在您的Ionic应用中实施推送。然后,在您的后端调用您选择的推送提供程序的API,向您的用户设备发送推送通知(您可以发送给受变化数据影响的所有用户或个人用户 - 您可以在后端决定)。您可以通过您的应用以任何方式处理该通知,包括呈现模式或类似内容。
Observable是否与推送通知不同? – user3215061
是的,Observable是不同的,更适合于代码中的逻辑流程。你在这里处理外部信息的问题,这在服务器端处理得更好。 – janpio