katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1.02k forks source link

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

Open ahsanhabib23 opened 5 years ago

ahsanhabib23 commented 5 years ago

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 (cordova-lib@8.1.1)
   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
sanitul commented 5 years ago

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

sanitul commented 5 years ago

I founded the solution at #430 disableBatteryOptimizations

not working

Flavioamorim commented 5 years ago

I have the same problem !

megadrazz commented 5 years ago

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

sanitul commented 5 years ago

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

Flavioamorim commented 5 years ago

For me not working :( !!

Flavioamorim commented 5 years ago

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 :( !

TDola commented 5 years ago

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

Neumann-Valle commented 5 years ago

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

Flavioamorim commented 5 years ago

@utan in this url, have a description of instalation

Neumann-Valle commented 5 years ago

@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..

Flavioamorim commented 5 years ago

@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 !

Neumann-Valle commented 5 years ago

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..?

Flavioamorim commented 5 years ago

@utan cordova 8.1.2 (cordova-lib@8.1.1)

Neumann-Valle commented 5 years ago

@Flavioamorim , so with this cordova 8.1.2 works?

TDola commented 5 years ago

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

Neumann-Valle commented 5 years ago

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(); }); `

TDola commented 5 years ago

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

Neumann-Valle commented 5 years ago

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

edento commented 5 years ago

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

zuck commented 5 years ago

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...

andreddosantos commented 4 years ago

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

Flavioamorim commented 4 years ago

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

andreddosantos commented 4 years ago

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

YotzinAlexander commented 4 years ago

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

Anyone already have any solution?

TDola commented 4 years ago

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

danielehrhardt commented 4 years ago

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?

TDola commented 4 years ago

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.

ahsanhabib23 commented 4 years ago

@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

zivlevy commented 3 years ago

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 ?

gregor-srdic commented 3 years ago

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

TDola commented 3 years ago

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.

maikonigor commented 3 years ago

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);
    });
riccardocostantini commented 10 months ago

@katzer