Geofencing no notifications

I am trying to use geofencing in one of my Ionic-3 apps. After floolwing the instructions in https://ionicframework.com/docs/native/geofence/, I modified my app.components.ts that now looks like this:

import { Geofence } from ‘@ionic-native/geofence’;

@Component({ templateUrl: ‘app.html’ }) export class MyApp {
constructor(private geofence: Geofence) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
this.startGeofence();
});
this.pages = [
]; }

startGeofence() {
// initialize the plugin
this.geofence.initialize().then(
// resolved promise does not return a value
() => {
console.log(‘Geofence Plugin Ready’);
this.addGeofence();
},
(err) => {
console.log(‘Geofence Plugin failed’);
console.log(err);
}
) }

private addGeofence() {
this.geofence.removeAll();
//options describing geofence

let fence = {
  id: '69ca1b88-6fbe-4e80-a4d4-ff4d3748acab', //any unique ID
  latitude: 41.2713354, //center of geofence radius
  longitude: 18.6108311,
  radius: 20, //radius to edge of geofence in meters
  transitionType: 3, //see 'Transition Types' below
  notification: { //notification settings
    id: 3, //any unique ID
    title: 'Geofencing', //notification title
    text: 'my home', //notification body
    openAppOnClick: true //open app when notification is tapped
  }
}

this.geofence.addOrUpdate(fence).then(
  () => console.log('Geofence added'),
  (err) => console.log('Geofence failed to add')
);

this.geofence.onTransitionReceived().subscribe(resp => {
  resp.forEach(function (geo) {
    console.log("Geofence transition detected", geo);
  });

  console.log("geofence on transition recieved", resp);
  alert(JSON.stringify(resp));
});   } }

In the console log I have the following messages:

deviceready has not fired after 5 seconds. Ionic Native: deviceready
event fired after 4964 ms Geofence Plugin Ready Geofence added

those show a correct initialization of the plugin, but, after that, nothing happens, the onTransitionReceived is never called.

My package.json:

{ “name”: “ClientIonicProject”, “version”: “0.0.1”, “author”:
“Ionic Framework”, “homepage”: “http://ionicframework.com/”,
“private”: true, “scripts”: {
“clean”: “ionic-app-scripts clean”,
“build”: “ionic-app-scripts build”,
“lint”: “ionic-app-scripts lint”,
“ionic:build”: “ionic-app-scripts build”,
“ionic:serve”: “ionic-app-scripts serve” }, “dependencies”: {
@angular/common”: “^5.0.1”,
@angular/compiler”: “^5.0.1”,
@angular/compiler-cli”: “^5.0.1”,
@angular/core”: “^5.0.1”,
@angular/forms”: “^5.0.1”,
@angular/http”: “^5.0.1”,
@angular/platform-browser”: “^5.0.1”,
@angular/platform-browser-dynamic”: “^5.0.1”,
@ionic-native/app-version”: “^4.4.0”,
@ionic-native/core”: “4.3.3”,
@ionic-native/geofence”: “^4.7.0”,
@ionic-native/geolocation”: “^4.4.0”,
@ionic-native/http”: “^4.4.0”,
@ionic-native/in-app-browser”: “^4.4.0”,
@ionic-native/launch-navigator”: “^4.4.0”,
@ionic-native/network”: “^4.5.2”,
@ionic-native/splash-screen”: “4.3.3”,
@ionic-native/status-bar”: “4.3.3”,
@ionic-native/text-to-speech”: “^4.4.0”,
@ionic/storage”: “^2.1.3”,
@ngx-translate/core”: “^8.0.0”,
@ngx-translate/http-loader”: “^2.0.0”,
“cordova-android”: “6.3.0”,
“cordova-plugin-actionsheet”: “^2.3.3”,
“cordova-plugin-add-swift-support”: “^1.6.0”,
“cordova-plugin-advanced-http”: “^1.11.1”,
“cordova-plugin-app-version”: “^0.1.9”,
“cordova-plugin-compat”: “^1.2.0”,
“cordova-plugin-device”: “^1.1.4”,
“cordova-plugin-dialogs”: “^2.0.1”,
“cordova-plugin-file”: “^6.0.1”,
“cordova-plugin-geofence”: “^0.7.0”,
“cordova-plugin-geolocation”: “^3.0.0”,
“cordova-plugin-inappbrowser”: “^1.7.2”,
“cordova-plugin-ionic”: “^3.1.3”,
“cordova-plugin-ionic-webview”: “^1.2.1”,
“cordova-plugin-network-information”: “^1.3.4”,
“cordova-plugin-splashscreen”: “^5.0.2”,
“cordova-plugin-statusbar”: “^2.4.2”,
“cordova-plugin-tts”: “^0.2.3”,
“cordova-plugin-whitelist”: “^1.3.1”,
“cordova-sqlite-storage”: “^2.3.1”,
“es6-promise-plugin”: “^4.2.2”,
“ionic-angular”: “3.9.2”,
“ionic-plugin-keyboard”: “^2.2.1”,
“ionicons”: “3.0.0”,
“rxjs”: “5.5.2”,
“sw-toolbox”: “3.6.0”,
“uk.co.workingedge.phonegap.plugin.launchnavigator”: “^4.2.0”,
“zone.js”: “0.8.18” }, “devDependencies”: {
@ionic/app-scripts”: “3.1.0”,
“typescript”: “2.4.2” }, “description”: “An Ionic project”, “cordova”: {
“plugins”: {
“cordova-plugin-tts”: {},
“cordova-plugin-app-version”: {},
“cordova-plugin-geolocation”: {
“GEOLOCATION_USAGE_DESCRIPTION”: “To locate you”
},
“cordova-plugin-inappbrowser”: {},
“uk.co.workingedge.phonegap.plugin.launchnavigator”: {},
“cordova-plugin-advanced-http”: {},
“ionic-plugin-keyboard”: {},
“cordova-plugin-whitelist”: {},
“cordova-plugin-device”: {},
“cordova-plugin-ionic-webview”: {},
“cordova-plugin-network-information”: {},
“cordova-sqlite-storage”: {},
“cordova-plugin-geofence”: {},
“cordova-plugin-add-swift-support”: {},
“cordova-plugin-statusbar”: {},
“cordova-plugin-splashscreen”: {}
},
“platforms”: [
“android”
] } }

ionic info:

cli packages: (C:\Users\francesco\AppData\Roaming\npm\node_modules)

@ionic/cli-utils  : 1.19.2
ionic (Ionic CLI) : 3.20.0

global packages:

cordova (Cordova CLI) : 8.0.0

local packages:

@ionic/app-scripts : 3.1.0
Cordova Platforms  : android 6.3.0
Ionic Framework    : ionic-angular 3.9.2

System:

Android SDK Tools : 26.1.1
Node              : v6.10.3
npm               : 6.0.0
OS                : Windows 10

Environment Variables:

ANDROID_HOME : D:\e\programmi_esterni\Android\android-sdk

Misc:

backend : legacy

Suggestions?

Did you find a solution for this? I’m struggling with the same problem and can’t find any helpful results when Searching the web

Likewise. Please let us know if you’ve found anything

nothing to do. I am blocked on this.

I had this problem and managed to solve it. But it was all a bit of a nightmare as it involved modifying a java file that the plugin installs.

I’m also using the phonegap-plugin-push plugin and on Android 8 or above you should now create a channel (or several channels) for your app to use. You can set different priorities on each channel if you want. This lets your users decide which, if any, notifications they want to receive. There’s a rundown of these new features in this Android 8.0 Features and APIs doc.

Basically, as far as I can tell, the implementation of the notification builder class in the plugin Java file is not compatible with Android 8 or above as there is no channel id or priority set. I’m basing this upon this Android guide.

Anyway, I cloned the geofence plugin, modified the ‘GeoNotificationNotifier.java’ file in the cloned plugin Android src folder, and installed the plugin from my local source. Worked for my particular use case…

1 Like

Hi. I can ask you please, send me the code that you modified to see if it serves me. I have to make geofence work in an app in my work and I have not managed to work on an android 8 I have several days trying to do that. you comment that you did it by modifying a .java file
thanks in advance for your help

I actually stopped using geofence notifications in my app as the new ‘optimisations’ of Android mean that the actual event just wasn’t being emitted when the fence was transitioned. It could actually be delayed by anything from 15 - 60 minutes! No use for my use case.

However, I have had a dig around and the lines I altered in the ‘GeoNotifcationNotifier.java’ file were to do with building the notification (starting at line 27). In the original they do not specify a notification channel id or a notification priority:

public void notify(Notification notification) {
    notification.setContext(context);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
        .setVibrate(notification.getVibrate())
        .setSmallIcon(notification.getSmallIcon())
        .setLargeIcon(notification.getLargeIcon())
        .setAutoCancel(true)
        .setContentTitle(notification.getTitle())
        .setContentText(notification.getText());

So I just updated the builder config to include the notification id and the notification priority:

public void notify(Notification notification) {
    notification.setContext(context);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, notification.getChannelid())
        .setVibrate(notification.getVibrate())
        .setSmallIcon(notification.getSmallIcon())
        .setLargeIcon(notification.getLargeIcon())
        .setAutoCancel(true)
        .setContentTitle(notification.getTitle())
        .setContentText(notification.getText())
        .setPriority(notification.getImportance());

This in turn meant that the priority has to be passed to the builder (the ‘notification.getChannelid()’ and
‘notification.getImportance()’), which is achieved by modifying the ‘Notification.java’ file. At around line 22 add:

@Expose public String channelid;
@Expose public int importance;

at around line 40 or thereabouts add:

public String getChannelid() {
    return this.channelid;
}

public int getImportance() {
    return this.importance;
}

Then in your Ionic service you need to pass the ‘channelId’ and the ‘importance’ as properties of your notification object.

You will have also had to create a notification channel so that you can use that as the ‘channelid’. I’m afraid you’ll have to research how to do that, in my case I was also using the ‘phonegap-plugin-push’ plugin which does this.

This is as far as I went, but I hope it helps …

Thank you very much, so I can begin to understand the problem and move forward

Any breakthrough or solution to this? Thanks