Need Help in setInterval() for updating Local Notification but when App goes in background, notification won't get updated with latest information ?!


#1

I’m tracking current position and wants to update the position when app is in background. This functionality is good while app is in foreground. But on Background notification is going well but not with updated position/Coordinates.

In this part of code, I’ve a call to doNotify() function which takes name of place to Local notification functionality.

this.task = setInterval(() => {
      this.infoWindow = new google.maps.InfoWindow;
if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
          var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
          };
          console.log("My Latitude: " + position.coords.latitude);
          console.log("My Longitude: " + position.coords.longitude);

          that.http.get('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + position.coords.latitude + ',' + position.coords.longitude + '&key=AIzaSyCnFxcopJ1KvuCfnz2mHgUiZmmKd3eidEE').map(res => res.json()).subscribe(data => {
            that.place = data.results[0].address_components[1]['long_name'];
          });
        
          that.infoWindow.setPosition(pos);
          that.infoWindow.setContent(that.place);
          that.infoWindow.open(that.map);
          that.map.setCenter(pos);
          console.log('In GOogle track fn '+that.place);
          that.doNotify(that.place);
        }, function () {
          this.handleLocationError(true, that.infoWindow, that.map.getCenter(), that.map);
        });
      } else {
        // Browser doesn't support Geolocation
        this.handleLocationError(false, that.infoWindow, that.map.getCenter(), that.map);
      }
    }, 7000);

doNotify Method which is receiving place as a parameter named locate

doNotify(locate){
    this.platform.ready().then(() => {
      console.log('Mera Notify '+locate);
      this.localNotifications.schedule({
        title: 'Food App Alert',
        text: 'Delivery boy location: '+locate+' ',
        every: "second",
        at: new Date(new Date().getTime() + 7000),
        data: {"title": locate}
      });
    });
  }

For notification on background using platform.ready i’m doing this

that.platform.ready().then(() => {
      console.log('Construct call '+that.place);
      that.localNotifications.on("click", (noti, state)=>{
        alert(state);
        alert(noti.data);
      });
    });

Note: Notification is doing good and receiving after intervel but the only thing going wrong is that data is not going to get update when position changes.

Thanks in Advance