katzer / cordova-plugin-background-mode

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

an issue with using the ackgroundMode.on('active') event #313

Open nemr opened 7 years ago

nemr commented 7 years ago

Hello ,

I'm trying to disable the background mode after 10 seconds in the background.

I'm trying to display an alert and then disable background mode . non of them are happing , the alert doesn't show up and it sill works in the background.

Am I implementing it correctly ? or is it limited to some specific actions when in background.

I'm developing for iOS

`var app = { // Application Constructor initialize: function() { document.addEventListener('deviceready', this.onDeviceReady.bind(this), false); },

// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
    this.receivedEvent('deviceready');
    cordova.plugins.backgroundMode.enable();

    cordova.plugins.backgroundMode.on('active', function () {
                                      setTimeout(function () {
                                                 alert("Background mode will be disabled")
                                                 cordova.plugins.backgroundMode.setEnabled(false);
                                                 }, 1000);
                                      });

},

// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);

}

};

app.initialize();

`

also if I only enable background and console log it won't print but if I only console log without enabling background it will console log

` onDeviceReady: function() { this.receivedEvent('deviceready'); cordova.plugins.backgroundMode.enable(); console.log("Background Enabled") // doesn't console log

},`

` onDeviceReady: function() { this.receivedEvent('deviceready'); console.log("Background Enabled") // does console log

},`
mvdhoek1 commented 6 years ago

Alert() doesn't work in background modus, the plugin uses the following code for that: cordova.plugins.backgroundMode.setDefaults({ title: String, text: String, icon: 'icon' // this will look for icon.png in platforms/android/res/drawable|mipmap color: String // hex format like 'F14F4D' resume: Boolean, hidden: Boolean, bigText: Boolean })

I'm still learning to work with this plugin but I think this will make a change ;)