ToniKorin/cordova-plugin-autostart

Starting the background

andersborgabiro opened this issue ยท 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

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.

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.

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

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

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

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

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

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

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

@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);
}

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

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

@smchae oh thanks, im using ionic3 as well.

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

@jorantos please help me :(

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

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

@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

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

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

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"