4

I'm using Ionic Framework to build an Android/iPhone cookbook app with Angular.

One of the features should be alarms/timers for each ingredient/step.

Ionic is based on a wrapper around Apache Cordova, I guess. So I've tried using a Cordova plugin, from an answer below... but it's older and it's not specifically for Ionic, and I've tried a few times but it just hasn't worked for me. I haven't found any alternatives.

  doSomething() {

    let successCallback = () => window.confirm('success');
    let errorCallback = () => window.confirm('fail');

    // set wakeup timer
    (<any>window).plugins.wakeuptimer.wakeup(successCallback,
      errorCallback,
      // a list of alarms to set
      {
          alarms : [{
              type : 'onetime',
              time : { seconds  : 5 },
              extra : { message : 'json containing app-specific information to be posted when alarm triggers' },
              message : 'Alarm has expired!'
          }]
      }
    );
  }

Local notifications are kind of an option, but it's just a notification. It doesn't work like a native alarm clock. (It's like getting a text, but you miss it so your burgers burn.)

Can anyone give an example of an alarm or timer in a modern Ionic app? Maybe I'm just missing something to get the plugin to work?

Or is there any kind of alternative I'm missing?

I'm getting bummed that the app is almost done but I've been stuck on this. :(

RJB
  • 2,063
  • 5
  • 29
  • 34

3 Answers3

4

You need an alarm plugin. this one can work: https://github.com/Haggus/cordova-plugin-wakeuptimer

to set an alarm :

// set wakeup timer
window.wakeuptimer.wakeup( successCallback,
   errorCallback,
   // a list of alarms to set
   {
        alarms : [{
            type : 'onetime',
            time : { hour : 14, minute : 30 },
            extra : { message : 'json containing app-specific information to be posted when alarm triggers' },
            message : 'Alarm has expired!'
       }]
   }
);
devseo
  • 1,182
  • 1
  • 13
  • 26
  • Looks useful, thanks, but I haven't been able to get it to work with Ionic... I've updated the question – RJB Feb 20 '20 at 03:41
3

The plugin you are attempting to use is almost 5 years old. Older than Ionic 2 if you using Ionic v3 and you try to install the plugin will return this erore

enter image description here

so the best solution for you use Local Notifications

Good luck.

Younes Zaidi
  • 1,180
  • 1
  • 8
  • 25
0

As Younes Zaidi already mentioned in his answer, the cordova-plugin-wakeuptimer (originally from wnyc, Haggus just did a fork) is old, no longer actively maintained and fails to install.

I managed to get it to work though (for Android only! - I haven't tried iOS at all - maybe it works maybe it doesn't), so if you still want this, continue reading ;-)

The install error is partially due to the lack of a package.json in the repository and partially to some general problems that cordova seems to have with installing packages directly from GitHub. There has been a lot of ranting on several sites about this - see for example https://github.com/Telerik-Verified-Plugins/ImagePicker/issues/55 <- The comments there also include some suggestions on how to solve this & references to the respective cordova versions.

I myself have solved it by using scripts that manipulate my app's package.json and config.xml. (This "solution" is available as a docker-compose project if you want to see it work before going into the trouble. Just go to https://github.com/lentschi/expiry_sync & there follow the "Building app & server with docker" part - or, if you don't want to execute it, view the two aforementioned files and see what I do here: setup/app/build_scripts/build-android-dev.sh)

However: This only worked until Android 10 came out - this now requires a special permission granted by the user. See the details @android.com.

So I created a fork of the original cordova-plugin-wakeuptimer supporting this, which I will not maintain - see the README there on how to request the required permissions: https://github.com/lentschi/cordova-plugin-wakeuptimer

lentschi
  • 300
  • 1
  • 11