cordova-plugin-autostart icon indicating copy to clipboard operation
cordova-plugin-autostart copied to clipboard

Starting the background

Open andersborgabiro opened this issue 8 years ago • 25 comments

I've seen this being discussed before, during January this year, but I'm wondering whether it was ever considered to support starting the app in the background. In my case the app will issue local notifications if it has something to say, so the user will be aware of that anyway, and can act on it.

Is there any other safe way to achieve that?

I'm not sure how the backbutton plugin could help.

Regards, Anders

andersborgabiro avatar Sep 07 '16 08:09 andersborgabiro

Seconded.

jameskennethrobinson avatar Sep 07 '16 22:09 jameskennethrobinson

Still in the TODO list as mentionend in #10 . And unfortunately the latest Android versions (6.x) seems to stop long running Javascript tasks after couple of hours even you are using the de.appplant.cordova.plugin.background-mode plugin. So I think at the moment only reliable solution is to use the native Android background service coded with Java.

ToniKorin avatar Sep 08 '16 03:09 ToniKorin

What might save me is that I use background geolocation. At least the app hasn't stopped after 6+ hours. I'll have it running continuously to see if it ever stops. So to me auto-start in the background is still of interest.

andersborgabiro avatar Sep 08 '16 04:09 andersborgabiro

Hi Andres, anyway the solution would be quite easy to implemented. Just a new SharedPreference variable to indicate the triggered auto-startup and a new Javascript method to read and clear it. If you need it urgently, feel free to contribute to this plugin.

At the moment I'm quite busy with other tasks...

ToniKorin avatar Sep 08 '16 05:09 ToniKorin

Sadly I am too (release on friday), but I will see if I understand the code enough to provide a solution.

andersborgabiro avatar Sep 08 '16 05:09 andersborgabiro

Please help and see how you can implement it, I want to use it on dev app which i intend to make production ready (release soon). @ToniKorin @andersborgabiro

ghost avatar Sep 10 '16 13:09 ghost

I apologize, but I'm probably not cut out for this. Right now I don't even know how to make a Cordova app start "minimized".

andersborgabiro avatar Sep 12 '16 19:09 andersborgabiro

When your app is noticing the auto-startup, just trigger the service of "cordova-plugin-backbutton" to minimize the app.

ToniKorin avatar Sep 12 '16 19:09 ToniKorin

A fast temporal solution its add this line in the AppStarter class of the plugin:

if( !className.equals("") ){ //Log.d("Cordova AppStarter", className); Intent serviceIntent = new Intent(); serviceIntent.setClassName(context, packageName + "." + className); serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); serviceIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); serviceIntent.getExtras().putBoolean("cdvStartInBackground",true);//this line context.startActivity(serviceIntent); }

And be sure that the cordova activity has this code: // enable Cordova apps to be started in the background Bundle extras = getIntent().getExtras(); if (extras != null && extras.getBoolean("cdvStartInBackground", false)) { moveTaskToBack(true); }

jorantos avatar Jan 12 '17 11:01 jorantos

@jorantos I am building an app with ionic and I am trying to follow your suggestion. Where should i put

Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
} 

this part?

I have added serviceIntent.getExtras().putBoolean("cdvStartInBackground",true); to AppStarter and the app still starts on foreground after reboot.

smchae avatar Apr 12 '17 07:04 smchae

@smchae In the MainActivity.java.

And better than "serviceIntent.getExtras().putBoolean("cdvStartInBackground",true);" in AppStarter, use serviceIntent.putExtra("cdvStartInBackground",true) . Because getExtras can be null.

I tested the app just now and works.

jorantos avatar Apr 18 '17 10:04 jorantos

@jorantos it works

paredesivan avatar May 08 '17 19:05 paredesivan

any updates on this if we can use the JS method to start in background/foreground?

kodeine avatar Jun 12 '17 23:06 kodeine

@jorantos can you tell me where exactly should i put the following? Because there's no MainActivity.java file in this repo.

And be sure that the cordova activity has this code:
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}

kodeine avatar Jul 08 '17 20:07 kodeine

@paredesivan @smchae were you guys able to fix this?

kodeine avatar Jul 11 '17 05:07 kodeine

@kodeine I am using Ionic. MainActivity.java is created when you build. Its under platforms/src/com/whatever your package name is

smchae avatar Jul 11 '17 05:07 smchae

@smchae oh thanks, im using ionic3 as well.

kodeine avatar Jul 11 '17 06:07 kodeine

@smchae this work fine in first time but if reboot device again app open in foreground mode

alimhv avatar Jul 15 '17 14:07 alimhv

@jorantos please help me :(

alimhv avatar Jul 16 '17 10:07 alimhv

in BootCompletedReceiver.java file replace
appStarter.run(context, intent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED); with this appStarter.run(context, intent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED); work fine for me

alimhv avatar Jul 16 '17 13:07 alimhv

@ali-mahmoodvand not for me, the app is still opening in foreground.

luizvnasc avatar Nov 30 '17 11:11 luizvnasc

@ToniKorin Could you give some hints on how to write a Cordova Plugin that solves this issue and is compatible with cordova-plugin-autostart? I have never written a Cordova Plugin

SimoneMSR avatar May 15 '18 14:05 SimoneMSR

Hello, does anyone has a working solution working in javascript side? I am using Ionic and have not access to native Java

LucaColombi avatar Aug 29 '18 12:08 LucaColombi

Hello, does anyone has a working solution working in javascript side? I am using Ionic and have not access to native Java

tenes que ir a la ruta src donde esta el codigo de java de android, modificarlo, quitar el plugin y agregarlo nuevamente con el codigo actualizado

paredesivan avatar Sep 19 '18 13:09 paredesivan

Hello, does anyone has a working solution working in javascript side? I am using Ionic and have not access to native Java

tenes que ir a la ruta src donde esta el codigo de java de android, modificarlo, quitar el plugin y agregarlo nuevamente con el codigo actualizado

you mean "you have to go to the src path where is the android java code, modify it, remove the plugin and add it again with the updated code"

SimoneMSR avatar Sep 19 '18 15:09 SimoneMSR