Running service in background when app is not active , is it possible?

How to run a service in background ? i want to run a service in background to track user location and send Notifications !

Any plugins available ?

1 Like

I think this will be helpfull.http://ngcordova.com/docs/plugins/backgroundGeolocation/. I never checked but i come across this. Please check and if it works please reply back.

1 Like

I came accross that one …!! but free version is only available for ios i guess !! and premium version is costly…!! i needed some free alternative or atleast a way to run native code through cordova…

check this one. This one is free https://www.npmjs.com/package/cordova-plugin-mauron85-background-geolocation

1 Like

thanks i will try this !! :grinning: but its still a fork of christocracy one with the paid version ! i am not sure…!! I will try this…

Thanks For liking.Is it working??

not yet but i wil reply as soon as i try… (y)

Hi, have you tried it ?

M.

@sriteja09 @ziobudda Actually , tommorow i am going to try this !! Till now i was just gathering resources and completing my half project !!

now i need to implement this. However , now i have only one confusion in my mind since i am new to angular.

If any of you good with angular … can you please tell how to wrap the above script in angular form ?

I am not sure how to wrap that geolocation script with ionic app.

help is appreciated…!!

@sriteja09 @ziobudda

I Tried This…
It’s Working…

Used This Config Just for testing…

backgroundGeoLocation.configure(callbackFn, failureFn, {
        desiredAccuracy: 0,
        stationaryRadius: 5,
        distanceFilter: 5,
        debug: true, // <-- enable this hear sounds for background-geolocation life-cycle.
        stopOnTerminate: false, // <-- enable this to clear background location settings when the app    terminates
        notificationTitle: 'Testing GeoLocation',
        notificationText: 'Successfully Implemented',
            //locationService: backgroundGeoLocation.service.ANDROID_FUSED_LOCATION
    });

However,

  • It works when app is minimized , but whenever i kill the app It Stops and notification also disappears.

  • I don’t know technical difference between ANDROID_FUSED_LOCATION and ANDROID_DISTANCE_FILTER , but the things i observed was ANDROID_DISTANCE_FILTER updates locations frequently and ANDROID_FUSED_LOCATION didn’t.

This , works only when I put code in $ionicPlatform.ready function of app.js.

The problem is App kill. How to make it work even when app is not active ? Help please…

Part 1 : the geolocation: it’s better to use watchPosition for a couple of seconds than to use getCurrentPosition. ( use watchPosition for 15 or 25 seconds or so… keeping coordinates as they come in only if the accuracy is better than the last coordinate. then stop it and keep the last, most accurate coordinate. ) Repeat this again when you want to capture the position later.

Part 2 : keeping the application ‘alive’ even when the phone is ‘sleeping’ :

you need the Background-mode plugin :

cordova plugin add de.appplant.cordova.plugin.background-mode

and my version of the Power Management Plugin

cordova plugin add https://github.com/boltex/cordova-plugin-powermanagement.git

Then in your app.js “run” method of your main angular module, you put this in the $ionicPlatform.ready function :

if( ionic.Platform.isAndroid() ){
      cordova.plugins.backgroundMode.enable();

      window.powerManagement.dim(function() {
          console.log('Wakelock acquired');
      }, function() {
          console.log('Failed to acquire wakelock');
      });
      window.powerManagement.setReleaseOnPause(false, function() {
          console.log('setReleaseOnPause successfully');
      }, function() {
          console.log('Failed to set');
      });

}

There… you’re done. The application will remain “alive” and trigger all your timeouts even when the screen turns black and the phone is ‘sleeping’.

If you also needed some push notifications it will be messed up… so you need to use another plugin that I modified specially for this.

cordova plugin add https://github.com/boltex/PushPlugin.git

ask me again if you need it = Hope this helps :smile:

1 Like

I have tried this katzer’s background plugin to see if i can execute that geolocatin plugin !! But fr some reason that normal katzer plugin’s test notification is not showing !!

I did this in my ionic platoform ready

cordova.plugins.backgroundMode.enable();

then below

cordova.plugins.backgroundMode.setDefaults({
title:  'text',
ticker: 'text',
text:   'text'
})

Then below…

cordova.plugins.backgroundMode.onactivate = function () {
   //  I tried some previous working geolocation stuff here.
}

With all this , i think that background mode notification should have appeared… bt it doesn’t !!

what is missing ?

Thanks a lot for your reply ! and is that powermanagement plugin only fr android ?

Yep all this is for android only.

what do you mean by “that normal katzer plugin’s test notification” ?

The geolocation plugin i am using also has its own notification to display when its working !!

And katzer’s plugin also has its own notification to appear when its in background mode !! Which is set like this …

cordova.plugins.backgroundMode.setDefaults({
title:  'text',
ticker: 'text',
text:   'text'
})

So in ideal case if all is working i should have two notifications one of geolocation plugin and other of katzer’s .

Now, if this notification is not showing i think something is wrong with my config.

Did you solve your question about “keeping the application ‘alive’ all time”? If yes, could you share if plugin cordova-plugin-background-mode works as you wish?

Regards

This plugin works in background all the time :slight_smile: Only the issue is if you need to get user updates frequently when terminated you will have to write custom code in java !

Since in android when in background plugin will store locations in database , and later on those are used when app is opened !

I wanted the updates immediately in background so i had to write custom background code.

Thanks. This plugin is a fork of deprecated “cordova-plugin-background-geolocation”, so I don’t know if it’s a good idea have it in your project…

Did you try to use cordova-plugin-background-app (cordova-plugin-background-app - npm)?

The purpose of this plugin is to enable notifications, alarms, etc to re-start your app and fire callbacks without the app causing any visual cues to the user. In essense, it allows starting an app as an Android service, but does so without the use of …

even i was not sure about that so i avoided christocracy’s ANDROID_DISTANCE_FILTER
and i used ANDROID_FUSED_LOCATION as a service .

i tried cordova-plugin-background-app plugin but it never worked after app termination ! and also apps with this plugin were getting rejected by ios app store according to some issues.

Hey, Sumeet123. I noticed you said you wrote “custom background code in java” to have the callback fired in background instead of just persisiting the data. Could you please share this? I would greatly appreciate it! :slight_smile:

yes i did it ! but my code wont send callback back to cordova/js. in java itself all all work is done. where a common database is shared between cordova and java files.

so if you dont need your callback to reach back to cordova then i can help. for my particular requirement i dont need that.