cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

Background Mode is running for only few(probably 5 mins) minutes in Android 8 and Android 9

Open ahsanhabib23 opened this issue 6 years ago • 34 comments

The App is running for almost 5 minutes at the background. After that, it stops working. If I resume then it starts again.

I have tried several solutions. I have also used the plugin https://github.com/DavidBriglio/cordova-plugin-foreground-service and added cordova.plugins.foregroundService.start('GPS Running', 'Background Service', 'myicon', 3, 10); cordova.plugins.backgroundMode.disableBatteryOptimizations(); cordova.plugins.backgroundMode.disableWebViewOptimizations();

But it did not work. I also tried with Cordova power management. This is my GitHub repo https://github.com/ahsanhabib23/testBackgroundIonic3 where the issue has been reproduced.

I am using the fork version. Most added the library by running this command cordova plugin add https://github.com/katzer/cordova-plugin-background-mode --nofetch

I know there is an existing open issue. But still posting because comments from there did not help.

Ionic:

   ionic (Ionic CLI)  : 4.12.0 (/usr/local/lib/node_modules/ionic)
   Ionic Framework    : ionic-angular 3.9.5
   @ionic/app-scripts : 3.2.2

Cordova:

   cordova (Cordova CLI) : 8.1.2 ([email protected])
   Cordova Platforms     : android 7.1.4
   Cordova Plugins       : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 6 other plugins)

System:

   Android SDK Tools : 26.1.1 (/Users/bs236/Library/Android/sdk/)
   ios-deploy        : 1.9.2
   NodeJS            : v10.15.3 (/usr/local/bin/node)
   npm               : 6.9.0
   OS                : macOS High Sierra
   Xcode             : Xcode 10.1 Build version 10B61

ahsanhabib23 avatar Jul 25 '19 15:07 ahsanhabib23

I founded the solution at https://github.com/katzer/cordova-plugin-background-mode/issues/430 disableBatteryOptimizations

sanitul avatar Jul 26 '19 07:07 sanitul

I founded the solution at #430 disableBatteryOptimizations

not working

sanitul avatar Jul 26 '19 08:07 sanitul

I have the same problem !

Flavioamorim avatar Jul 26 '19 17:07 Flavioamorim

Solução crie uma service com uma notificação passos!!!!! index.js >> receivedEvent, onDeviceReady

antes cordova.plugins.backgroundMode.setDefaults({ title: "title", text: "text", icon: 'icon', // this will look for icon.png in platforms/android/res/drawable|mipmap color: "6E10A0", // hex format like 'F14F4D' resume: false, silent: false, hidden: false, bigText: false, })

       >>> aqui    cordova.plugins.backgroundMode.enable();
      >>> antes   cordova.plugins.backgroundMode.onactivate = function () {
                    cordova.plugins.backgroundMode.disableWebViewOptimizations();
                };
                cordova.plugins.backgroundMode.ondeactivate = function () {
                    alert('teste')
                };
    >>>> aqui      cordova.plugins.backgroundMode.disableBatteryOptimizations();

plugin.mxl add permission

inicie o service com uma notificação pegue parte do codigo plugin powemanger e inclua em seu service ele vai trabalha para servir cordova.plugins.backgroundMode para manter ele sempre ativo! abraços!!!!! testado Android 8 e Android 9 rodando aqui 24hs por dias! vlw

megadrazz avatar Jul 27 '19 15:07 megadrazz

Solução crie uma service com uma notificação passos!!!!! index.js >> receivedEvent, onDeviceReady

antes cordova.plugins.backgroundMode.setDefaults({ title: "title", text: "text", icon: 'icon', // this will look for icon.png in platforms/android/res/drawable|mipmap color: "6E10A0", // hex format like 'F14F4D' resume: false, silent: false, hidden: false, bigText: false, })

       >>> aqui    cordova.plugins.backgroundMode.enable();
      >>> antes   cordova.plugins.backgroundMode.onactivate = function () {
                    cordova.plugins.backgroundMode.disableWebViewOptimizations();
                };
                cordova.plugins.backgroundMode.ondeactivate = function () {
                    alert('teste')
                };
    >>>> aqui      cordova.plugins.backgroundMode.disableBatteryOptimizations();

plugin.mxl add permission

inicie o service com uma notificação pegue parte do codigo plugin powemanger e inclua em seu service ele vai trabalha para servir cordova.plugins.backgroundMode para manter ele sempre ativo! abraços!!!!! testado Android 8 e Android 9 rodando aqui 24hs por dias! vlw

Haa ótima ideia :) @ahsanhabib23

sanitul avatar Jul 27 '19 17:07 sanitul

For me not working :( !!

Flavioamorim avatar Aug 12 '19 19:08 Flavioamorim

if i set targetSdkVersion="26" or targetSdkVersion="27" this plugins works, but the Google not will accepted API smaller than 28 !! and with targetSdkVersion="28" not working :( !

Flavioamorim avatar Aug 14 '19 17:08 Flavioamorim

Do you still have the issue if you use this plugin? https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background

TDola avatar Aug 15 '19 13:08 TDola

@TDola , I cant even get the plugin from that url.

Neumann-Valle avatar Aug 20 '19 07:08 Neumann-Valle

@utan in this url, have a description of instalation

Flavioamorim avatar Aug 20 '19 11:08 Flavioamorim

@TDola , Yes I indeed ran the installation procedure , it failed with the following:

`Failed to fetch plugin https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background via registry. Probably this is either a connection problem, or plugin spec is incorrect. Check your connection and plugin name/version/URL. Error: npm: Command failed with exit code 1 Error output: npm ERR! path git npm ERR! code ENOENT npm ERR! errno ENOENT npm ERR! syscall spawn git npm ERR! enoent Error while executing: npm ERR! enoent undefined ls-remote -h -t https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background.git npm ERR! enoent npm ERR! enoent npm ERR! enoent spawn git ENOENT npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent

npm ERR! A complete log of this run can be found in: npm ERR! /home/utan-dev/.npm/_logs/2019-08-20T13_23_10_949Z-debug.log `

Opps disregard, forgot I switched to Ubuntu and didn't have Git installed yet..

Neumann-Valle avatar Aug 20 '19 13:08 Neumann-Valle

@utan do you tryed to create a simple installation and testing this plugin? i haved this error, but, when i create a new app with same version, works !

Flavioamorim avatar Aug 20 '19 13:08 Flavioamorim

Ok, the plugin got installed.. But It won't stay connected once go into background mode..

I have cordova @9.. any other way to make this work..?

Neumann-Valle avatar Aug 20 '19 13:08 Neumann-Valle

@utan cordova 8.1.2 ([email protected])

Flavioamorim avatar Aug 20 '19 13:08 Flavioamorim

@Flavioamorim , so with this cordova 8.1.2 works?

Neumann-Valle avatar Aug 20 '19 17:08 Neumann-Valle

Works fine in cordova 9, be sure to set the disable battery and webview optimizations, otherwise Android Pie shuts down anything that runs too long. These features were fixed in the fork The BosZ made

TDola avatar Aug 20 '19 17:08 TDola

Hi` @TDola I have that! do you mean disableWebVIewOptimization:method ?

`js cordova.plugins.backgroundMode.enable();

cordova.plugins.backgroundMode.on('activate', function() { cordova.plugins.backgroundMode.disableWebViewOptimizations(); }); `

Neumann-Valle avatar Aug 20 '19 17:08 Neumann-Valle

yes that should be all that's needed. If its not working as it should, post a new issue here https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background

TDola avatar Aug 20 '19 17:08 TDola

@TDola , I will, I had it wrapped in a timeout of 500 mils .. will test without it.. and see.

Neumann-Valle avatar Aug 20 '19 17:08 Neumann-Valle

You can fix that with cordova-plugin-background-geolocation, if that's an option for you.

edento avatar Aug 27 '19 14:08 edento

Same issue here, working for 5min on a Xiaomi device with Android One.

I also tried to add a setInterval which attempts to do a cordova.plugins['backgroundMode'].moveToForeground() but without success...

zuck avatar Sep 11 '19 15:09 zuck

Same issue here, the app stops after 5 minutes. Tested every sugestion above..

andreddosantos avatar Sep 18 '19 10:09 andreddosantos

@andreddosantos did you try https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background ?

Flavioamorim avatar Sep 18 '19 17:09 Flavioamorim

Yes while in background i used to call a sqliteplugin to persist some coordinates and now after five minute the calls to the sqlite just pile in the callstack

andreddosantos avatar Sep 18 '19 18:09 andreddosantos

I have the same problem, after 5 minutes the application stops, on android 8 and 9.

Anyone already have any solution?

YotzinAlexander avatar Sep 20 '19 20:09 YotzinAlexander

Not sure, my own usage is designed to give up after 15 minutes anyways as to not kill the users battery. But in theory I am calling configure a lot. I use the notification text as a progress indicator. So the frequent updates, once per 20 seconds roughly, might be tricking Android into keeping it alive?

My usage is basically this: On app pause event -> Check a queue, if anything important is in the queue, activate background mode As queue runs, it updates background mode with config. The queue is essentially a setTimeout that recursively calls itself and burns down a list of tasks in a sqllite database, updating as it goes, and uploading a file. Every successful loop causes the config call. If the queue runs out of important tasks, even if there is still more tasks in it, it tells background mode to shut down. On some phones the app keeps going anyways, on others it stops immediately.

In the case of iOS, I use background fetch instead. It's the same basic principal, except it shuts itself down after 25 seconds as to not use too much time on iOS and risk the OS killing the process.

Background mode is broken in iOS and won't be accepted to the app store anymore

TDola avatar Sep 20 '19 20:09 TDola

Not sure, my own usage is designed to give up after 15 minutes anyways as to not kill the users battery. But in theory I am calling configure a lot. I use the notification text as a progress indicator. So the frequent updates, once per 20 seconds roughly, might be tricking Android into keeping it alive?

My usage is basically this: On app pause event -> Check a queue, if anything important is in the queue, activate background mode As queue runs, it updates background mode with config. The queue is essentially a setTimeout that recursively calls itself and burns down a list of tasks in a sqllite database, updating as it goes, and uploading a file. Every successful loop causes the config call. If the queue runs out of important tasks, even if there is still more tasks in it, it tells background mode to shut down. On some phones the app keeps going anyways, on others it stops immediately.

In the case of iOS, I use background fetch instead. It's the same basic principal, except it shuts itself down after 25 seconds as to not use too much time on iOS and risk the OS killing the process.

Background mode is broken in iOS and won't be accepted to the app store anymore

Did you found a Solution?

danielehrhardt avatar Oct 25 '19 23:10 danielehrhardt

Not sure, my own usage is designed to give up after 15 minutes anyways as to not kill the users battery. But in theory I am calling configure a lot. I use the notification text as a progress indicator. So the frequent updates, once per 20 seconds roughly, might be tricking Android into keeping it alive? My usage is basically this: On app pause event -> Check a queue, if anything important is in the queue, activate background mode As queue runs, it updates background mode with config. The queue is essentially a setTimeout that recursively calls itself and burns down a list of tasks in a sqllite database, updating as it goes, and uploading a file. Every successful loop causes the config call. If the queue runs out of important tasks, even if there is still more tasks in it, it tells background mode to shut down. On some phones the app keeps going anyways, on others it stops immediately. In the case of iOS, I use background fetch instead. It's the same basic principal, except it shuts itself down after 25 seconds as to not use too much time on iOS and risk the OS killing the process. Background mode is broken in iOS and won't be accepted to the app store anymore

Did you found a Solution?

To it only working for 5 minutes? Far as I can tell, as long as there is activity to the notification, it keeps working. Try calling configure in a setTimeout loop to update the notifications periodically and see if it still stops running after 5 minutes.

TDola avatar Oct 28 '19 12:10 TDola

@megadrazz

Your solution worked well for me. But all of a sudden it does not work. Additionally, whenever I go to the background, I console.log some data inside cordova.plugins.backgroundMode.onactivate and cordova.plugins.backgroundMode.ondeactivate. So whenever I go back and forth, nothing is printing

ahsanhabib23 avatar Oct 29 '19 10:10 ahsanhabib23

same problem here. 5 min and no more XHR calls from the background. still getting geo location updates but the call to the server is blocked by the system. any solution ?

zivlevy avatar Sep 15 '20 12:09 zivlevy

do you request android.permission.ACCESS_BACKGROUND_LOCATION in your MainActivity ?

gregor-srdic avatar Oct 22 '20 06:10 gregor-srdic

If you can, you might want to reconsider tracking in the background. https://developer.android.com/about/versions/11/privacy/location https://developer.android.com/training/location/permissions#request-background-location As noted above, google is cracking down on it. Your app must now either request foreground, or background location access.

TDola avatar Oct 22 '20 18:10 TDola

You can call the wakeUp method in a given interval to keep the app awake.

const timer = 4*60*1000; // four minutes
 this.backgroundMode.enable();
 this.backgroundMode.on('activate')
    .subscribe(()=>{
       setInterval(()=>{
            this.backgroundMode.wakeUp();
            this.backgroundMode.moveToForeground();
          },timer);
    });

maikonigor avatar Jun 02 '21 19:06 maikonigor