BackgroundMode.enable() is undefined

Hi all ! Could anyone suggest please … what I did wrong ? I write in app.componet.ts file such code … but in all console.log I see undefined

platform.ready().then(() => {

  document.addEventListener('deviceready', function () {



    console.log("cordova.plugins.backgroundMode.isActive",cordova.plugins.backgroundMode.isActive() );

    console.log(" cordova.plugins.backgroundMode.wakeUp()", cordova.plugins.backgroundMode.wakeUp() );

    console.log("cordova.plugins.backgroundMode.unlock()",  cordova.plugins.backgroundMode.unlock() );



  }, false);


Try wrapping your code with a small timeout. Use setTimeout(() => { ... }, 500)

Sometimes the deviceready event fires before plugins are ready, not sure why but it happens some times.

ihadeed, thanks for answer… but it is not understandable plugin for me … it always return false and dose not working …

~~~ Deleted response, Wrong convo ~~~

Yes,I am using Ionic 2 … but problem in background plugin, not in transfer …

Oops. Sorry I replied to the wrong thread.

Well I’m looking at what you’re doing here.

You are console logging the output of cordova.plugins.backgroundMode.enable(). The method does not return anything. The method takes callback functions that will be called on success or error.

To really check if the plugin exists or not, you would want to console log cordova.plugins.backgroundMode.enable without ().

ihadeed, If I console.log(cordova.plugins.backgroundMode.enable), I see function exports.enable()

Yeah so the plugin is working correctly.

These functions don’t return anything. So when you console log the output, you will always get undefined.

isActive() should return a boolean though.

ihadeed, isActive() return false …

Well that means the plugin works.

The reason why you’re getting false is because you’re not giving the plugin some time to process your request.

.enable() is an asynchronous function that goes to the native layer of the application. .isActive() is a synchronous function that will return a value immediately based on the state stored in the JavaScript part of the plugin.

The correct way to use the plugin is to first call the .enable() method, then wait for the enable even to fire before doing anything else:

cordova.plugins.backgroundMode.on('enable', function() { ... });

After the enable event fires, call cordova.plugins.backgroundMode.isEnabled() and it should return true. Now you can start calling other plugin methods and doing stuff.

ihadeed, I am trying something like this, but see cordova.plugins.backgroundMode.isEnabled() return is false.

  platform.ready().then(() => {
    setTimeout(() => {
      document.addEventListener('deviceready', function () {
        cordova.plugins.backgroundMode.on('enable', function() {
          console.log("cordova.plugins.backgroundMode.enable();", cordova.plugins.backgroundMode.isEnabled() );
      }, false);

    }, 500);

According to the source code, that shouldn’t happen. The enable event is firing after the native executes the callback function.

I suggest you open an issue at their repo to get help with this, as it doesn’t seem like how the plugin should behave.

ihadeed,thanks … I will do …