I am trying to send notifications everyday from my app using LocalNotification plugin that I found at github. I have the following code which sends a notification as soon as the application is started.
var notification = cordova.require("cordova/plugin/localNotification");
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
alert('device ready');
var id = 0;
id++;
newDate = new Date();
newDate.setUTCHours(1,30,1);
notification.add({
id : id,
date : newDate,
message : "Your message here",
subtitle: "Your subtitle here",
ticker : "Ticker text here",
repeatDaily : true
});
}
But I want the application to automatically send notification without being opened. Setting the option repeatDaily to true will help ?
I did my research and found out that others were able to achieve it using the LocalNotification plugin.
I am not quite sure of how to test since it requires me to keep the AVD powered on for one full day. The objective is very simple. I need to send out a single notification everyday to a user without opening the app. Any help will be highly appreciated !! Thanks !!
I have never used the plugin myself but a little digging into the code shows me that yes as long as you set
repeatDaily
totrue
your notification will be there every day.If you take a look on the AlarmHelper class you can see the if clause for that parameter setting to repeat every day.
One extra detail explained on the AlarmReceiver class is that if you set the time for a previous time, (e.g. now is 11:00 and you set the alarm to repeat every day at 08:00) it will fire immediately, and then in the next day on the scheduled time. So that class has an if clause to prevent that.
To set the date, you use the
date
param. In your sample you're usingnew Date()
which returns by default the current datetime, and your notification will be displayed daily at the same time. If you want to specify a different time for your alarm, pass in a date object with the desired time!EDIT
An easy way of making sure your code is running only once is using localstorage.
And just make sure to clear the variable if you ever unset your alarm: