在检查数据json API时实时发出本地通知

时间:2019-04-25 09:39:27

标签: angular notifications ionic4

我正在使用离子框架制作应用程序。我的应用程序满足了航班时间表,一切正常。我已经放置了按钮,当用户单击该按钮时,他将收到有关所需航班状态的本地通知。我习惯于cordova插件local notificationsBackground mode 。 l使用背景模式插件使我的应用程序无限执行背景执行来检查飞行状态,然后向用户发送本地通知。 l if表达式用于检查到达的航班状态,如果不继续循环则推送通知。我什至使用set Interval来保持数据重新加载以检查飞行状态。我执行了所有这些步骤,即使到达了飞行状态,我也没有收到任何通知

完整代码

   export class FlightserachdetailsPage implements OnInit {


      public flight: any
      public myFlag = true;

      callsign = null
      airport = null
      status = null
      aircraft = null
      airline = null
      time = null
      logo = null
      id: any
      myStatus: boolean;

      constructor(private http: HTTP, public loadingController: LoadingController,
        private nav: NavController, private activatedRoute: ActivatedRoute, private backgroundMode: BackgroundMode
        , private localNotifications: LocalNotifications, public alertController: AlertController) {

        this.activatedRoute.params.subscribe((params) => {
          this.callsign = params['callsign'];
          this.airport = params['airport'];
          this.time = params['time'];
          this.aircraft = params['aircraft'];
          this.status = params['status'];
          this.airline = params['airline'];
          this.logo = params['logo'];
          this.id = params['id'];

          if (this.status == "landed") {
            this.myFlag = true
            console.log(this.myFlag)
          } else {
            this.myFlag = false
          }
          if (this.status == "estimated") {
            this.myStatus = true
            console.log(this.myFlag)
          } else {
            this.myStatus = false
          }


        });


        this.backgroundMode.enable()

      }


      ngOnInit() {


      }

///Alert confirm to receive notification   

      async RecNotifi() {

        const alert = await this.alertController.create({
          header: 'Confirm!',
          message: 'Are you sure you do want receive notification about flight ' + this.callsign,
          buttons: [
            {
              text: 'Cancel',
              role: 'cancel',
              cssClass: 'secondary',
              handler: (blah) => {
                console.log('Confirm Cancel: blah');
              }
            }, {
              text: 'Okay',
              handler: () => {
                console.log('Confirm Okay');

                this.ActiveNotif()

              }
            }
          ]
        });

        await alert.present();

      }

//// execute to active code check for flight status  

      async ActiveNotif() {


        this.http.get('xxxxxxxxxxxxxx.json?flightId=' + this.id + '', {}, {})
        .then(data => {

          let FlightDetails = JSON.parse(data.data).result.response.data.flight
          let identification = FlightDetails.identification.callsign
          let status = FlightDetails.status.generic.status.text

          if (status == "landed") {

            this.localNotifications.schedule({
              title: 'Your flight has arrived',
              text: 'Your flight '+identification + 'is arrived',
              foreground: true
          });

          }

          setInterval(()=> {
            this.ActiveNotif()
          }, 4000);  


          console.log('notifiction is running ',data.data)
        })

      }

      navigate(id, callsign, status) {
        this.nav.navigateForward(`/map-track-befor/${id}/${callsign}/${status}`);
      }


      handleImgError(ev: any, item: any) {
        let source = ev.srcElement;
        let imgSrc = `/no__logo.png`;
        console.log(imgSrc)
        source.src = imgSrc;

      }

    }

有什么想法吗?

0 个答案:

没有答案