Using cordova-plugin-background-mode


Desmond Hyde

Hi,

I am attempting to use the cordova-plugin-background-mode in App builder

I have managed to install plugin in the MyApp_Compiled/plugins folder by using CALL cordova plugin add cordova-plugin-background-mode as an after batch transaction

I have then used the jscript feature in the same way you have in your cordovaplugin sample whereby i have a script title test1 which contains 2 lines

i run the test1 script by linking it to a button click event after the app device is ready or loaded

Yet I continuously get the following error when running it tells me cordova is undefined,

If I run this in a simulation as a device the script does not run

please help :)


desh123

DecSoft

Hello Desmond,

You mention that you get an error... but don't specify the error message! Anyway, I receive an E-Mail from you (I want to reply it after reply this post) in which you use the below line in the App Options -> Cordova -> Batch -> After:

I am not sure about the error (please, if this answer don't solve the problem, post here the error message that you get!), but, probably its an installation problem, since the above line must be changed by the below one, which is the correct:

Another way can be to use the App Options -> Cordova -> General -> Custom plugins. If you want to use this way, then you must specify in the Custom plugins input only the plugin ID, without any other thing, for example:

Please, try the above ways of installations of the Cordova plugin that you want to use, then try again with the plugin's API, and, if you have any error, please, refer here the error message in order to try to help you.

P.S. Remember that the Cordova API and the Cordova's plugins API are available inside the app's Ready event or after that event: if you try to use before that event, Cordova nor their plugins are not available and therefore you can get some unexpected behaviour or error.



Desmond Hyde
Hi David I used your last suggesttion (Custom) cordova-plugin-better-background-mode and the Cordova.backgroundMode is now installed to the plugins folder but I am still getting the error Error: "Unable to get property 'setEnabled' of undefined or null reference" - Line: 1260 - Char: 1 - Code: 0 - Url: http://127.0.0.1:9999/app/scripts/app.js on the below script which I am running in the ready event App.Cordova.backgroundMode.setEnabled(true); Alert('Hi');
desh123

DecSoft

Hello Desmond,

What the error message means? That we are trying to access to some "setEnabled" property (or method) that is not available in "App.Cordova.backgroundMode". This already tell us something useful...

If we take a look at the plugin's documentation, apparently we must use one of the below methods:

And that must work... so the error is more or less clear here: we are trying to use a "setEnabled" method which is not present in the variable that we are specifying. Try with one of the above methods, taken from the plugin's documentation.



Desmond Hyde
Hi David when using cordova.plugins.backgroundMode.enable(); Error: "'cordova' is undefined" - Line: 1258 - Char: 1 - Code: 0 - Url: http://127.0.0.1:9999/app/scripts/app.js when using cordova.plugins.backgroundMode.setEnabled(true); Error: "'cordova' is undefined" - Line: 1260 - Char: 1 - Code: 0 - Url: http://127.0.0.1:9999/app/scripts/app.js
desh123

DecSoft

Hello Desmond,

Maybe you are trying to execute the app in a browser? If so, certainly, "cordova" is not defined... "cordova" is defined (and ready to use) if you try the app in an Android device, for example.

If you want your app running in Cordova platforms and also in browsers, or just avoid the issue in browsers, then you must take care before use the Cordova's stuff, for example, with the below code in your app's Ready event:



Desmond Hyde
Hi David I ran the cordova_android_run.bat file into device simulator if the code works then Alert('Hi'); would activate, but it does not
desh123

DecSoft

Hello Desmond,

There a couple of questions here. First, maybe the emulator don't support the Cordova plugin... or viceversa, the plugin can't work in the emulator. My recomendation for this is that you simply avoid the emulator: connect a device to your computer via USB, and then, the AB Run BAT file just launch the app in your device, not the emulator.

The other thing here is the code that you want to execute... your code looks like this:

But probably that code is wrong... Javascript is case sensitive, and, probably you want to use "alert", not "Alert".



Desmond Hyde
Hi David Where is the AB Run BAT file located
desh123

DecSoft

Hello Desmond,

Humm... maybe I confuse to you... I refer to the BAT file that AB generates when you compile the app. Inside the "_Compiled" folder, you must see the "cordova_android_run.bat" file. This is the RUN file that I am talking about.



Desmond Hyde
Hi David No luck using it on Android my device, alert message doe snot display which means the cordova error is not recognised
desh123

DecSoft

Hello Desmond,

Please, copy here the code that you are trying to use, and indicate where you place that code (the app's Ready event, etc.). I take a look to prepare some sample for you, but, certainly the plugin is not as easy as can appear in principle... the plugin provide several methods, events, etc.



DecSoft

Hello Desmond,

Below is an small sample that works as expected. Please, try it. Copy the below text and save it in a file named like "MyApp.ab"... then open that file with App Builder and compile the app: also with Apache Cordova.

Look at the app's Ready event, in which I place the sample code, including the "alert" that we want to view.



Desmond Hyde

Hi David

Thanks for the input but Im afraid to say this doe snto work on the device

if you use your code then the alert works

but if you use the below code the alert does not work which tell me an error runs when the cordova.plugins.backgroundMode.setEnabled(true); is initiated

Does the code work on your device using the below ?


desh123

DecSoft

Hello Desmond,

You are right, however, this is quite rare... according to the plugin documentation, we can use something like what we wanted:

However, if we try something like these alerts:

All of them appear to be undefined... so certainly the below code can't work as expected:

Even more... since the code fail... if we place an alert after that the above line of code, we can't see the alert too, since the error interrupt the script execution.

Honestly I don't know what to say... we are following the plugin's documentation, so, if they don't work as expected... we are missing something here... maybe you can find a possible alternative for this plugin? Or contact with the author? Maybe search in Google about the problem... because certainly the plugin appear to be installed properly, but, then, at least one of their **available** methods are not available...

Maybe we are missing here something? Maybe we can's use that method in the app's Ready event? :-(



DecSoft

Hello Desmond,

Looking into this, I found this issue in the plugin's Github project page.

Apparently there is a problem or bug in the plugin that cause that we cannot get it working in Android, at least. If you read the linked issue, some user suggest to remove the below line from the "\MyApp_Compiled\plugins\cordova-plugin-background-mode\plugin.xml" file:

Certainly, doing that apparently solve the problem, that is, at least the "enabled" method works, and, also the plugins' events. Please, copy the below code into a file and save it as "MyApp.ab", and then take a look at the app's Ready event.

Certainly it's not common to edit plugin's files, but, apparently this is required for this specific plugin... until the problem or bug has been fixed. I recommend you to try with the above app, to read the plugin's documetation, the possible issues, etc.



Desmond Hyde
Hi David The alerts for various events are now working. I was looking at other alternatives like trying to introduce wakelocks etc into the mainactivity.xml files But considering the alerts now work it is fair to assume the plugin is now working. I will now try to process data in the background. Your commitment to this project is really appreciated and your service is top class Thank you so much I will continue to spread the word about your product. Regards Des
desh123

DecSoft

Hello Desmond,

Thanks for your kindly words, sir. My only fear with this plugin is that the plugin's author advert that the Google Store can probably not allow apps that made tasks in background... here is the paragraph that I refer:

Plugin for the Cordova framework to perform infinite background execution.

Most mobile operating systems are multitasking capable, but most apps dont need to run while in background and not present for the user. Therefore they pause the app in background mode and resume the app before switching to foreground mode. The system keeps all network connections open while in background, but does not deliver the data until the app resumes.

Store Compliance

Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.

Use the plugin by your own risk!

Of course, not all the apps must be distributed along the Stores, so, if you plain to distribute your app in another way, for example, by just downloading the APK file from your website, then probably you can use the plugin without worries about the possible problems in the regular Stores.


Todo el mundo puede leer el foro de soporte de DecSoft para aprender del mismo, sin embargo, sólo los clientes de DecSoft pueden abrir nuevos hilos. Compre una o más licencias de productos de DecSoft y obtendrá este y otros beneficios.

Este sitio utiliza "cookies" útiles para almacenar sus preferencias.

Bien. Ocultar esta nota. Obtener más información.