Bring app to focus


#1

Hello.

I have an application with a countdown, that starts playing a sound when the countdown is done.
It even starts playing the alarm when the app is running in the background, which is actually good.
However, the sound starts playing, with the app still running in background.

What I need is for the app to automatically come back to focus when the alarm goes off.
Is there any built in feature for this, or does anyone know about a cordova plugin which handles my need?


#2

Take a look at this:


#3

Awesome, That seems to be exactly what I was looking for.
However, being farely new to Ionic, implementing a cordova plugin seems to be a bit out of my reach.

I have installed the plugin to my application using “ionic plugin add …” and the plugin is now located in my plugins folder, but due to the lack of documentation for this plugin Im having a hard time implementing it in my app.

I tried the following:

angular.module(‘TimeIt’, [‘ionic’, ‘tabSlideBox’, ‘TimeIt.controllers’, ‘LocalStorageModule’, ‘PowerManagement’])

Which threw an error saying that the “PowerManagement” app wasnt installed or was misspelled. Can anyone point me in somewhat of the right direction to use this plugin with javascript in my controllers?

Thanks.


#4

In vanilla JS you can use the plugin as follows:

window.plugins.PowerManagement.acquire( successCallback, errorCallback );
window.plugins.PowerManagement.release( successCallback, errorCallback );
window.plugins.PowerManagement.dim( successCallback, errorCallback ); 

From cordova 1.6+ plugin architecture has changed pretty extreme, you have to do:

cordova.require('cordova/plugin/powermanagement').acquire( successCallback, errorCallback ); 
cordova.require('cordova/plugin/powermanagement').release( successCallback, errorCallback ); 
cordova.require('cordova/plugin/powermanagement').dim( successCallback, errorCallback ); 

Changing this to a ngCordovoa like factory, you would end up with something like this:

.factory( '$cordovaPowerManagement', [
  '$q',
  '$window',
  function ( $q, $window ) {
    return {

      acquire: function () {
        var q = $q.defer();
        $window.plugins.PowerManagement.acquire( function ( result ) {
          q.resolve( result );
        }, function ( err ) {
          q.reject( err );
        } );
        return q.promise;
      },
      release: function () {
        var q = $q.defer();
        $window.plugins.PowerManagement.release( function ( result ) {
          q.resolve( result );
        }, function ( err ) {
          q.reject( err );
        } );
        return q.promise;
      },
      dim    : function () {
        var q = $q.defer();
        $window.plugins.PowerManagement.dim( function ( result ) {
          q.resolve( result );
        }, function ( err ) {
          q.reject( err );
        } );
        return q.promise;
      }
    };
  }
] );

You could add this factory to your app or controller, and you could then inject $cordovaPowerManagement to your controller, and use the functions, functions will return a promise.

Take a note: I did not test above code, I just assembled this from the plugin documentation and took some code snippets from ngCordova… Please let me know if it works!

Hope this helps :slight_smile:


#5

Thank you man, you are awesome. I suddenly got a much deeper understanding of how Ionic works with Cordova aswell :smile:
I had to modify the PowerManager plugin somewhat to suit my needs, as it was missing support for the “PARTIAL_WAKE_LOCK”, and “ACQUIRE_CAUSES_WAKEUP” and now it works like charm.

However, I still have two issues regarding this which this plugin does not solve. I need the app to open even if the mobile screen is locked, so that the user can turn of the alarm without unlocking the phone.

Secondly, I need the app to gain focus when the alarm goes off. For example if Im sitting on Facebook when the alarm starts, it switches to my app.

Ill google a bit for those.

I really appreciate the help you gave me :slight_smile:


#6

Hey @vimzzz you’re welcome! Personally, I learned very much about angular and ionic just by reading every single problem to the forum… Not that I had the same questions, but just to see the solutions - just like you said, I gained a lot of deeper understanding from doing this :wink:

Did you change the native code for your missing support, or did I just not include them in the factory snippet? I think future visitors might benefit if you could submit your final code to this thread, it really seems like an interesting set of requirements to have, and espescially if you put your final choice of plugins down here :slight_smile: This might absolutely be something I’d use for a future project, so at least I’m curious for future reference :wink:

Glad it helped in any case :slight_smile: