I’m creating an alarm clock feature in my app. To implement the feature, I’m using $cordovaLocalNotification, but I’m getting inconsistent results. The event only seem to be picked on occasionally. I have tried setting listeners for all the events in $cordovaLocalNotification, and put a console log in each of them. I only see these firing occasionally.
Ionic info gives me: Cordova CLI: 6.0.0 Gulp version: CLI version 3.9.0 Gulp local: Local version 3.9.1 Ionic Version: 1.2.4 Ionic CLI Version: 1.7.14 Ionic App Lib Version: 0.7.0 ios-deploy version: 1.8.2 ios-sim version: 4.1.1 OS: Mac OS X El Capitan Node Version: v5.7.1 Xcode version: Xcode 7.3 Build version 7D175
I know the $cordovaLocalNotification.schedule(options); is working, because if I place a $cordovaLocalNotification.getAll() in a timeout 3 seconds after the notifications are set, I can see them:
Object at: 1461134444 badge: 0 data: "{\"foo\":\"bar\", \"bar\":\"foo\"}" id: 4450001 sound: "res://platform_default" text: "my text" title: "my title"
If I place the app in the background, I can see the notification appear every time.
In addition, I found that if I added “every: 0” to the config object for the local notification, because I don’t want the notifications to repeat, the app crashed in the simulator. I don’t know if that’s related.
For reference, here’s a sample of my schedule() and $on() functions:
Create schedule (pushId is integer, alarmTime is timestamp,
var options = {
id: pushId,
title: 'My title',
text: 'my text',
at: alarmTime,
data: {
foo: 'bar',
bar: 'foo',
}
};
if (alarmTime > new Date()) { // only schedule if alarmTime is in the future
$cordovaLocalNotification.schedule(options);
}
Check for schedule, click and trigger (this is in main.js):
$rootScope.$on('$cordovaLocalNotification:schedule',
function (event, notification, state) {
console.log('schedule');
});
$rootScope.$on('$cordovaLocalNotification:trigger',
function (event, notification, state) {
console.log('trigger');
processTriggeredAlarm(event, notification, state);
});
$rootScope.$on('$cordovaLocalNotification:click',
function (event, notification, state) {
console.log('click');
processTriggeredAlarm(event, notification, state);
});
I also have $on for all of the other events, for debugging, but none of them appear, and the processTriggeredAlarm() function is never called.
Is this perhaps linked to something other than the plugin itself? I have nested loops with two-way bindings (bad, I know), and about 15 listeners (although they do not seem to be playing up). Perhaps angular is getting overloaded in some way and these listeners are being dropped from an internal stack, or something? Although the app seems perfectly usable and has not lost any functionality since trying to add this alarm with $cordovaLocalNotification.
I’m pretty sure I found the cause of the problem. I rebuilt the project entirely again, step by step, to see what was breaking it.
The Geolocation module was at fault, and was breaking the functionality. As soon as I added the GeoLocation module, things broke again. Just removing the the Geolocation module did not fix the issue, and it remained broken. I had to rebuild again.
I’ll do some more testing on a new test project to see what’s going on and will make a bug report if I find anything.
The Geolocation plugin was cordova-plugin-geolocation.
We decided to drop the geolocation feature, only to find that the local notification did not suffice anyway - we wanted to create an alarm feature that would also work when the app was in the background or the device was asleep, but if we set a custom sound file to the notification then it would only play after the user trapped the notification - which is totally useless, and the default tone of a single beep on iPhone (which would correctly play as soon as the notification appeared) would not wake up anyone but the lightest sleeper.
In the end, we are back to square one, with geolocation but dropped the new alarm feature. I’m afraid I haven’t had the time to do a full investigation and bug report yet.