Background service in ionic

Hi,
I’m using ionic v2 ,I need to create an app (Android) which send and receive data from php server …
I want to do this even if the user kill the application…
Can it be done by ionic ?.
If so how?

This is not possible with any development framework. Explicit killing of apps… kills them.

2 Likes

i mean when the app is closed ,
for example ,like whatsapp it will automatically download messages and send message /images when there is internet connection ,even the application not running

Use cordova plugin background mode

Yes, this might be a problem to be solved with https://ionicframework.com/docs/native/background-mode/. But again, this only works when the app is running.

What you see for Whatsapp and many other messengers are just push notifications that your phone receives. The actually message are only loaded when you start the app.

4 Likes

Background mode only work when the application is open,
And i don’t think with this i could achieve what am aiming for!

I’m unsure what your definition of background is then, but as Sujan12 has said it sounds like the solution you’re looking for is push notifications.

Think about this from a user perspective. Would you want an app that is constantly chewing up your battery when it isn’t even running?

1 Like

Background mode will execute when the user will open the app once atleast. Then if they tap on home button or exit the app, the background plugin will take over and create a persistent notification in android. this will keep the webview working for your hybrid app. Checkout the repo of the plugin for example. I created an app which runs in background on android.

For something like whatsapp, they use push notifications.

My aim is to upload some images to the server i’m able to do this when the user in the app/screen ;But if the user close the app or minimize the app i cloudn’t do it…
i want to achieve this,even the app is closed by user the image should be sent to server if there is internet connection…By using the background mode plugin ,Can i do this?

i am using background plugin to send the data to server when app is in minimize mode(no kill) as i keep my data in sqlite . You can do that

You need to experiment with this.

i will try … thanks

could you please tell me how to use the ON instance at ionic…
i have tried "this.backgroundMode.on(‘activate’, function () {

})

but it shows error

  this.backgroundMode.onactivate().subscribe(() => {
     
})

do something like this.

Try this

startTrackingLocation() {

    console.log('Start geotracking');

    console.log('Is background mode active: ',this.backgroundMode.isActive());
    this.backgroundMode.isActive() === true?this.trackGeoBackground():this.trackGeoForeground();



    this.backgroundMode.on('activate').subscribe(() => {


        this.stopTrackingGeoForeground();
        this.trackGeoBackground();
    });
    this.backgroundMode.on('deactivate').subscribe(() => {
        this.stopTrackingGeoBackground();
        this.trackGeoForeground();
    });

}

my app was working fine. I decided to install background mode plugin to process in background so that it will give a notification when user reached near certain location. But when i install the plugin and included in the module file and in my working file then app stops working without any error. Then I run my app removing this plugin and again it works fine. I am using ionic 3. I found somewhere that this plugin doesn’t work in ionic 3. Is this true? any solution please

1 Like

Did you find a solution? I need to send user’s coordinates to server even user kill the application. How should i do that? Can anyone help me?

Use the plugin

Every 10 Sec , This plugin update location coordinate to url

check out

1 Like

I check it. It can track even user kill the application. I need to send coordinates to my database. How can i do that while not running application. Do you have any idea?

1 Like