deckameron/Ti.Android.Geofence

Notifications are not received on Android in any app state.

Closed this issue · 18 comments

Environment Details:

  • Ti SDK 7.5.0
  • Target API Level Android 28
  • Nokia 6.1 - Android 9

I am able to get the events but only when app is running in foreground or in background state, but not receiving any notifications in killed state. May be it's due to Android 8/9 OS as I tested it only on Android 9 and don't have lower OS version devices.

Here's the sample code I am using:

var geofence = require("ti.android.geofence");
var locationAllowed = false;

function checkPermissions(cb){
   if (Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS)) {
       locationAllowed = true;
       (cb != undefined) && cb();

   } else {
       Ti.API.warn('Location permissions not granted! Asking now...');
       Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
           locationAllowed = e.success;
           (cb != undefined) && cb();
       });
   }
}

function addFences(){
    geofence.addGeofences({
        clearExistingFences : false,
        fences : [
{
                "id" : "office",
                "latitude" : 28.41259,
                "longitude" : 77.0425996,
                "radius" : 500,
                "transitions" : [ geofence.GEOFENCE_TRANSITION_ENTER, geofence.GEOFENCE_TRANSITION_DWELL, geofence.GEOFENCE_TRANSITION_EXIT ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "Office Address",
                'message' : 'I am at office address now.',
                "accentColor" : "#00AEE6",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            },
            {
                "id" : "huda_metro",
                "latitude" : 28.4615863,
                "longitude" : 77.062657,
                "radius" : 500,
                "transitions" : [ geofence.GEOFENCE_TRANSITION_ENTER, geofence.GEOFENCE_TRANSITION_DWELL, geofence.GEOFENCE_TRANSITION_EXIT ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "Huda City Centre",
                'message' : 'I am at Huda City Centre now.',
                "accentColor" : "#2AC635",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            },
            {
                "id" : "home",
                "latitude" : 28.4947681,
                "longitude" : 77.158355,
                "radius" : 500,
                "transitions" : [ geofence.GEOFENCE_TRANSITION_ENTER, geofence.GEOFENCE_TRANSITION_DWELL, geofence.GEOFENCE_TRANSITION_EXIT ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "My Home",
                'message' : 'I am at HOME now.',
                "accentColor" : "#D52C7D",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            }
        ]
    });

    startFencing();
}

function startFencing(){
    if (locationAllowed) {
        geofence.startMonitoring();
    }
}

function stopAllFencing(){
    geofence.clearExistingFences();
}

function stopMonitoring(){
    geofence.stopMonitoring();
}

geofence.addEventListener(geofence.ERROR, function(e){
    Ti.API.error("GEOFENCE ERROR");
    Ti.API.error(e.message);
});

geofence.addEventListener(geofence.ENTERED, function(e){
    alert("ENTERED");
    alert(JSON.stringify(e));
});

geofence.addEventListener(geofence.EXITED, function(e){
    alert("EXITED");
    alert(JSON.stringify(e));
});

geofence.addEventListener(geofence.DWELL, function(e){
    alert("DWELL");
    alert(JSON.stringify(e));
});

geofence.addEventListener(geofence.STARTED_MONITORING, function(e){
    alert("STARTED_MONITORING");
    alert("Monitoring...");
});

geofence.addEventListener(geofence.STOPPED_MONITORING, function(e){
    alert("STOPPED_MONITORING");
     alert("RIP!");
});

geofence.addEventListener(geofence.GEOFENCES_ADDED, function(e){
    alert("GEOFENCES_ADDED");
    alert("Ready to start monitoring!");
});

geofence.addEventListener(geofence.NOTIFICATION_CLICKED, function(e){
    alert("NOTIFICATION_CLICKED");
    alert(JSON.stringify(e));
});

checkPermissions(addFences);

I am testing the app nearby to physical location of first fence and whenever I spoof any other location to check exit or dwell events for any fence, I am not getting any events triggered up.

The only event triggered is ENTER and this is also fired only for the first location even if my current location is nearby to third geofence item.

I am spoofing the locations and getting correct co-ordinates for location updates in the Ti.Geolocation's location event.

Hi @prashantsaini1 ,

I am working on this issue right now but unfortunately I can not reproduce it. Please, take a look at the log below.

I used your code and locations.

[INFO] :   STARTED_MONITORING
[INFO] :   Monitoring...
[INFO] :   APSAnalyticsService: Analytics Service Started
[INFO] :   APSAnalyticsService: Stopping Analytics Service
[INFO] :   GeofenceTransitionsIS: Firing ENTERED...
[INFO] :   GeofenceModule: (AsyncTask #1) [122246,152700] Unable to find resource identifier to custom sicon : push
[INFO] :   GeofenceModule: (AsyncTask #1) [613,153313] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0
[INFO] :   ENTERED
[INFO] :   {"type":"ENTERED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"bgac":"#00AEE6","sicon":"push","alert":"I am at office address now.","title":"Office Address","event":"ENTERED","custom":[{"latitude":"28.412590","longitude":"77.042600","id":"office"}],"bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","ledc":"#FF009688","canNotify":true},"bubbles":false,"cancelBubble":false}
[INFO] :   APSAnalyticsService: Analytics Service Started
[INFO] :   APSAnalyticsService: Stopping Analytics Service
[INFO] :   GeofenceTransitionsIS: Firing ENTERED...
[INFO] :   GeofenceModule: (AsyncTask #2) [130417,283730] Unable to find resource identifier to custom sicon : push
[INFO] :   GeofenceModule: (AsyncTask #2) [653,284383] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4615863,77.062657&fov=120&pitch=0
[INFO] :   GeofenceTransitionsIS: Firing EXITED...
[INFO] :   ENTERED
[INFO] :   {"type":"ENTERED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"bgac":"#2AC635","sicon":"push","alert":"I am at Huda City Centre now.","title":"Huda City Centre","event":"ENTERED","custom":[{"latitude":"28.461586","longitude":"77.062657","id":"huda_metro"}],"bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4615863,77.062657&fov=120&pitch=0","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","ledc":"#FF009688","canNotify":true},"bubbles":false,"cancelBubble":false}
[INFO] :   GeofenceModule: (AsyncTask #2) [205,284588] Unable to find resource identifier to custom sicon : push
[INFO] :   GeofenceModule: (AsyncTask #2) [652,285240] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0
[INFO] :   EXITED
[INFO] :   {"type":"EXITED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"bgac":"#00AEE6","sicon":"push","alert":"I am at office address now.","title":"Office Address","event":"EXITED","custom":[{"latitude":"28.412590","longitude":"77.042600","id":"office"}],"bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","ledc":"#FF009688","canNotify":true},"bubbles":false,"cancelBubble":false}
[INFO] :   APSAnalyticsService: Analytics Service Started
[INFO] :   APSAnalyticsService: Stopping Analytics Service
[INFO] :   GeofenceTransitionsIS: Firing EXITED...
[INFO] :   GeofenceModule: (AsyncTask #3) [71326,356566] Unable to find resource identifier to custom sicon : push
[INFO] :   GeofenceModule: (AsyncTask #3) [548,357114] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4615863,77.062657&fov=120&pitch=0
[INFO] :   EXITED
[INFO] :   {"type":"EXITED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"bgac":"#2AC635","sicon":"push","alert":"I am at Huda City Centre now.","title":"Huda City Centre","event":"EXITED","custom":[{"latitude":"28.461586","longitude":"77.062657","id":"huda_metro"}],"bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4615863,77.062657&fov=120&pitch=0","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","ledc":"#FF009688","canNotify":true},"bubbles":false,"cancelBubble":false}
[INFO] :   GeofenceTransitionsIS: Firing ENTERED...
[INFO] :   GeofenceModule: (AsyncTask #4) [20965,378079] Unable to find resource identifier to custom sicon : push
[INFO] :   GeofenceModule: (AsyncTask #4) [606,378685] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4947681,77.158355&fov=120&pitch=0
[INFO] :   ENTERED
[INFO] :   {"type":"ENTERED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"bgac":"#D52C7D","sicon":"push","alert":"I am at HOME now.","title":"My Home","event":"ENTERED","custom":[{"latitude":"28.494768","longitude":"77.158355","id":"home"}],"bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4947681,77.158355&fov=120&pitch=0","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","ledc":"#FF009688","canNotify":true},"bubbles":false,"cancelBubble":false}

Notifications

In order to get fake locations used this app on my phone, a Nexus 6P with Android 9.
I also tested it on a genymotion emulator and used its own built in mock location functionality.

You don't need a key to use this module. It is completely free!

The code I used was this one below:

var geofence = require("ti.android.geofence");
var locationAllowed = false;

var window = Titanium.UI.createWindow({
    backgroundColor : '#F2F2F2',
    layout : 'vertical'
}); 

 window.addEventListener('open', function(){
    if (!Titanium.Geolocation.hasLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS)) {
        Titanium.API.warn('Location permissions not granted! Asking now...');
        Titanium.Geolocation.requestLocationPermissions(Titanium.Geolocation.AUTHORIZATION_ALWAYS, function(e) {
            if (!e.success) {
                Titanium.API.error('Location permissions declined!');
            } else {
                Titanium.API.info('Location permissions ready');
                // Initialize monitoring here
                locationAllowed = true;
            }
        });
    } else {
        Titanium.API.warn('Location permissions already granted!'); 
        // Initialize monitoring here
        locationAllowed = true;
    } 
});

var button_add = Titanium.UI.createButton({
    top : 24,
    title : 'Add Fences',
    backgroundColor : '#607D8B',
    color : '#FFF',
    width : 180
});
window.add(button_add);

button_add.addEventListener('click', function(){
    geofence.addGeofences({
        clearExistingFences : false,
        fences : [ 
            {
                "id" : "office",
                "latitude" : 28.41259,
                "longitude" : 77.0425996,
                "radius" : 500,
                "transitions" : [ 
                    geofence.GEOFENCE_TRANSITION_ENTER,
                    geofence.GEOFENCE_TRANSITION_DWELL, 
                    geofence.GEOFENCE_TRANSITION_EXIT
                ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "Office Address",
                'message' : 'I am at office address now.',
                "accentColor" : "#00AEE6",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            },
            {
                "id" : "huda_metro",
                "latitude" : 28.4615863,
                "longitude" : 77.062657,
                "radius" : 500,
                "transitions" : [ 
                    geofence.GEOFENCE_TRANSITION_ENTER,
                    geofence.GEOFENCE_TRANSITION_DWELL, 
                    geofence.GEOFENCE_TRANSITION_EXIT
                ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "Huda City Centre",
                'message' : 'I am at Huda City Centre now.',
                "accentColor" : "#2AC635",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            },
            {
                "id" : "home",
                "latitude" : 28.4947681,
                "longitude" : 77.158355,
                "radius" : 500,
                "transitions" : [ 
                    geofence.GEOFENCE_TRANSITION_ENTER,
                    geofence.GEOFENCE_TRANSITION_DWELL, 
                    geofence.GEOFENCE_TRANSITION_EXIT
                ],
                'dwellTime' : 5 * 60 * 1000,
                //Notification
                "title" : "My Home",
                'message' : 'I am at HOME now.',
                "accentColor" : "#D52C7D",
                'smallIcon' : 'push',
                "type" : geofence.TYPE_PLACE_FENCE,
                "showGooglePlaceBigImage" : true
            }
        ]
    });
});

var button_start = Titanium.UI.createButton({
    top : 24,
    title : 'Start Monitoring',
    backgroundColor : '#009688',
    color : '#FFF',
    width : 180
});
window.add(button_start);

button_start.addEventListener('click', function(){
    if(locationAllowed){
        geofence.startMonitoring();
    }
});

var button_clear = Titanium.UI.createButton({
    top : 24,
    title : 'Clear Fences',
    backgroundColor : '#F44336',
    color : '#FFF',
    width : 180
});
window.add(button_clear);

button_clear.addEventListener('click', function(){
    geofence.clearExistingFences();
});

var button_stop = Titanium.UI.createButton({
    top : 24,
    title : 'Stop Monitoring',
    backgroundColor : '#795548',
    color : '#FFF',
    width : 180
});
window.add(button_stop);

button_stop.addEventListener('click', function(){
    geofence.stopMonitoring();
});

geofence.addEventListener(geofence.ERROR, function(e){
    Titanium.API.error("GEOFENCE ERROR");
    Titanium.API.error(e.message);
});

geofence.addEventListener(geofence.ENTERED, function(e){
    Titanium.API.info("ENTERED");
    Titanium.API.info(JSON.stringify(e));
});

geofence.addEventListener(geofence.EXITED, function(e){
    Titanium.API.info("EXITED");
    Titanium.API.info(JSON.stringify(e));
});

geofence.addEventListener(geofence.DWELL, function(e){
    Titanium.API.info("DWELL");
    Titanium.API.info(JSON.stringify(e));
});

geofence.addEventListener(geofence.STARTED_MONITORING, function(e){
    Titanium.API.info("STARTED_MONITORING");
    Titanium.API.info("Monitoring...");
});

geofence.addEventListener(geofence.STOPPED_MONITORING, function(e){ 
    Titanium.API.info("STOPPED_MONITORING");
     Titanium.API.info("RIP!");
});

geofence.addEventListener(geofence.GEOFENCES_ADDED, function(e){
    Titanium.API.info("GEOFENCES_ADDED");
    Titanium.API.info("Ready to start monitoring!");
});

geofence.addEventListener(geofence.NOTIFICATION_CLICKED, function(e){
    Titanium.API.info("NOTIFICATION_CLICKED");
    Titanium.API.info(JSON.stringify(e));
});

window.open();

It is very simple code just for testing!
Please, let me know if you got it working.

Environment Details:

  • Ti SDK 7.5.0
  • Target API Level Android 25
  • Nexus 6P - Android 9
  • Module Version 1.0.3

You can download the zip file here.

The only difference I can see at present is the target api level. Mine is 28. Rest all is same. I'll try again after restarting my device because I'm still getting the ENTER event for Office location not sure how.

Please pay attention to your console log while compiling and see you can find any warnings from Appcelerator saying that this API Level is to new for them.

I believe API level 28 is officially supported by 7.5.0 as I'm not setting it and it's being set up by sdk automatically.

I uploaded it here

Could you send me your test project so I can try it as well?

I'll send the test project once I reach home as I'm traveling in same fences items. :D

This is strange!!! Try this one

I will investigate the notification issue.

I'll send you my sample test code after an hour once I reach home. For now, I doubt it's something to do with the API level + Ti SDK combination.
Note that my notifications for the sample app is on, so may be we need to specify some tags in manifest just like we did for FOREGROUND_SERVICE for sdk 7.5.x to run the app in foreground mode. Not sure exactly though, but I hope it helps to rectify the issue.

Last thing to note that my sample app is still running in background, it's not in killed state and still I'm not getting any notifications which I believe should be.

I also received the home location Enter event but only when I opened the app as it was in killed state earlier. So it seems that events are working fine except the notifications part.
FYI, I'm using Alloy app and may be the icon paths are causing the notification issue. That's why I didn't specify the largeIcon, and disabled the big Image. Only smallIcon is there in my sample code and the png file is inside app - platform - android - res - drawable folder.

@deckameron I reviewed your code files and the difference was only the Ti SDK. You are using SDK 7.0.0 and I am using 7.5.0 which I doubt is the cause of this behaviour as there could be some Android 28 API changes in 7.5.0 including the way it handles foreground and background services.

I could not test the app on SDK 7.0.0 as I do not have it installed and only using SDK 7.5.0. I was able to get all the events properly when app was running in background mode but still no notifications were shown in either background state or in killed state. I also noticed some weird logs on console which seems like some resource calls logs.

Here's the sample app I am Ti Geofence App

And here are the logs from console:

[INFO] :   ENTERED - {"type":"ENTERED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"alert":"I am at HOME now.","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","custom":[{"latitude":"28.494768","id":"home","longitude":"77.158355"}],"bgac":"#D52C7D","sicon":"push","ledc":"#FF009688","canNotify":true,"title":"My Home","event":"ENTERED","bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.4947681,77.158355&fov=120&pitch=0"},"bubbles":false,"cancelBubble":false}
[WARN] :   System: A resource failed to call close.
[WARN] :   System: A resource failed to call close.
[INFO] :   GeofenceModule: (AsyncTask #4) [853,376549] Unable to find image from url : https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0

[INFO] :   EXITED - {"type":"EXITED","source":{"apiName":"Ti.Module","bubbleParent":true,"invocationAPIs":[],"_events":{"ERROR":{},"ENTERED":{},"EXITED":{},"DWELL":{},"STARTED_MONITORING":{},"STOPPED_MONITORING":{},"STOPED_MONITORING":{},"NOTIFICATION_CLICKED":{}}},"fences":{"alert":"I am at office address now.","licon":"http://framework.sparklogix.com/wp-content/uploads/2016/01/globe1.png","custom":[{"latitude":"28.412590","id":"office","longitude":"77.042600"}],"bgac":"#00AEE6","sicon":"push","ledc":"#FF009688","canNotify":true,"title":"Office Address","event":"EXITED","bicon":"https://maps.googleapis.com/maps/api/streetview?size=512x256&location=28.41259,77.0425996&fov=120&pitch=0"},"bubbles":false,"cancelBubble":false}

Notice the [WARN] : System: A resource failed to call close. log as I saw it every time I received the events on console.

I hope this message helps to recognise the issue.

Hi @prashantsaini1,

Could you try using the "smallIcon" like this?

'smallIcon' : "@drawable/push"

instead of this

'smallIcon' : "push"

and remove showGooglePlaceBigImage for now.

Yes, I tried the same as you said, but I am still seeing the same logs on console. I also cloned your module code and ran it with some additional logs in sendNotification method and all the logs were printing properly till the end of that method. But somehow the notifications are not shown in any app state. Please note that I am still testing on same Environment Details as in this issue thread.

Finally I was able to reproduce to problem!
I will start working on it.

Hi @prashantsaini1 !

Could you try this version, please!
It is important to know that on Android 8 and 9 the geofencing has a latency of 2-3 minutes.