Trying to use cordova-plugin-wakeuptimer

Hi,

I’m trying to integrate https://github.com/wnyc/cordova-plugin-wakeuptimer into an Ionic app, but having issues getting it to work, and not found too muych info when googling.

cordova plugin add https://github.com/wnyc/cordova-plugin-wakeuptimer.git

just to make sure:

ionic platform rm ios
ionic platform add ios

If I understand correctly, Cordova should automatically add the plugin to the app, so that it’s available without having to include the plugin’s wakeup.js from the plugins/ dir

I’ve not created a factory wrapper yet, but I’m getting

Error: Can't find variable: ... 

or

Error: undefined is not an object (evaluating '...')

when running

ionic run ios --livereload --consolelogs

with any if the following in main.js

console.log($window.wakeuptimer.wakeup);
console.log(window.wakeuptimer.wakeup); // (obviously, but I tried just in case) 
console.log(wakeuptimer.wakeup);
console.log(Wakeup.wakeup);
console.log(wakeup.wakeup);
console.log($window.Wakeup.wakeup);
console.log($window.wakeup.wakeup);
console.log($window.wakeupplugin.wakeup);
.
.
.

I’m probably missing an obvious step, any suggestions?

I figured the the issue may have been because of dependency injection. So I Followed the patterns in ngCordova, and recreated the wrapper:

angular
.module('myApp')
.factory('wakeupWrapper', function () {

    return {
        wakeup: function(success, error, options) {
            return cordova.plugins.Wakeup.wakeup(success, error,options);
        },
        snooze: function(success, error, options) {
            return cordova.plugins.Wakeup.snooze(success, error, options);
        }
    };
});

I then added this into main.js as a dependency injection. I still get:

 error    Error: undefined is not an object (evaluating 'cordova.plugins.Wakeup')

Just to make sure, I also tried cordova.plugins.wakeup.

I don’t know this plugin, but check your app and be sure to use manual bootstrap like http://stackoverflow.com/questions/21556090/cordova-angularjs-device-ready

1 Like

Thanks Fabio, That solved it, and I also have a working, updated wrapper for anyone that needs it:

At the bottom of <HEAD>

<script type="application/javascript">
    window.ionic.Platform.ready(function() {
        angular.bootstrap(document, ['myApp']);
    });
</script>

Wrapper:

angular
    .module('myApp')
    .factory('wakeupWrapper', function () {
        return {
            wakeup: function(success, error, options) {
                return window.wakeuptimer.wakeup(success, error, options);
            },
            snooze: function(success, error, options) {
                return window.wakeuptimer.snooze(success, error, options);
            }
        };
    });