Geofence plugin is not working properly in iOS


#1

I am using geofence plugin in ionic2. Geofence plugin is working fine in Android and but in iOS, geofence is adding successfully but after entering or existing the geofence, notification is not coming.

and I am sending some local notification onTransitionReceived() is not working in iOS.

Here is my code

 private addGeofence(lat,lon){
    //options describing geofence
    let fence = {
      id: '69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb', //any unique ID
      latitude: this.latitude, //center of geofence radius
      longitude: this.longitude,
      radius: 100, //radius to edge of geofence in meters
      transitionType: 1, //see 'Transition Types' below
      // notification: { //notification settings
      //   id: 1, //any unique ID
      //   title: 'Title', //notification title
      //   text: "You are in customer's location. Tap to start your task.", //notification body
      //   openAppOnClick: true //open app when notification is tapped
      // }
    }
    this.geofence.addOrUpdate(fence).then(
      () => {
        console.log('Geofence added');
        //this.presentAlert('Geofence added');
        this.geofence.onTransitionReceived().subscribe(() => {
          //this.presentAlert('onTransitionReceived');
          this.scheduleNotification();
        });
      },
      (err) => {
        console.log('Geofence failed to add');
        this.presentAlert(err);
      }
    );
  }


scheduleNotification() {
    this.localnotification.schedule({
      id: 100,
      title: 'Tap to Start Task',
      text: "You are in to customer's location. Tap to start your task.",
      at: new Date(new Date().getTime() + 0.5 * 1000),
      //at:this.geofence.onTransitionReceived(),
      data: { mydata: "You are nearer to customer's location. Tap to start your task." }
    });
  }

Please help me out to resolve this issue.

Thanks in advance.


#2

Please provide a link to the geofence plugin you are using.
Also provide the output of ionic info and the content of package.json.

Is geofence not working or only the local notification not coming? Two quite different problems…


#3

Here is the plugin link that I am using.

https://github.com/cowbell/cordova-plugin-geofence

and my ionic info is given below

Cordova CLI: 7.0.1 
Ionic Framework Version: 3.4.2
Ionic CLI Version: 2.2.3
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.7
ios-deploy version: 1.9.1 
ios-sim version: 5.0.13 
OS: macOS Sierra
Node Version: v7.9.0
Xcode version: Xcode 8.1 Build version 8B62

and my package.json contains the below

"@ionic-native/core": "3.10.2",
"@ionic-native/geofence": "^3.12.1",
"@ionic-native/geolocation": "^3.12.1",
"@ionic-native/local-notifications": "^3.12.1"

Local notifications are working fine. But, In my case geofence is adding fine , I got to know that geofence is working with this addOrUpdate method. this method is adding the geofence for me but on entering or existing the geofence notifications are not coming.


#4

So you are sure geofence is working? Did you test by adding some logging?
Because then you don’t have a geofence problem, but a notification problem and this topic title and information is all wrong.


#5

I am using this method and present some alert in this method.

this.geofence.addOrUpdate(fence).then(
      () => {
        console.log('Geofence added');
        this.presentAlert('Geofence added');
      });

when I added geofence, then my alert is presenting so that i confirmed that geofence is adding.

As I mentioned, geofence plugin is not working properly in iOS in the sense It is not firing notification while entering or existing the fence.


#6

But this is not a problem of geofence but of notification.
If you get the alert from geofence, nothing is wrong with your geofence code.

Test your local notification code outside of the context of the geofence, behind a click of a button for example to make sure it works at all.


#7

FYI, geofence will fire its own notification while we enter into the fence or exit from the fence. I am talking about those notifications and are not working and those notifications are the part of geofence plugin.


#8

Are you talking about [quote=“umasankar421, post:1, topic:95073”]
// notification: { //notification settings
// id: 1, //any unique ID
// title: ‘Title’, //notification title
// text: “You are in customer’s location. Tap to start your task.”, //notification body
// openAppOnClick: true //open app when notification is tapped
// }
[/quote]

?

Is this why you added additional localnotification to “fix” this?


#9

Yes.

What I am doing is …

while we enter into the fence, geofence will call this onTransitionReceived() method. and I am firing some local notifcations in this method.

Problem is now, this method is not calling even after entering into the fence.

Any idea regarding onTransitionReceived() method will work on iOS?


#10
  1. Why aren’t you using the notification offered by the geofence stuff itself?
  2. So onTransitionReceived is not working at all? Are you sure it has to be called inside the other method and not by itself?

(Mixing the local notification and geofence problems together is kinda making this very complicated…)


#11

A post was split to a new topic: Geofence plugin not working on iOS


#12

#13

Thanks for creating the new post, I split it into its own new topic.

Please add the other information I asked for (plugins used, package.json, ionic info) to that post.