5

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 !!

bala
  • 436
  • 1
  • 4
  • 19

1 Answers1

4

I have never used the plugin myself but a little digging into the code shows me that yes as long as you set repeatDaily to true 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.

final AlarmManager am = getAlarmManager();

...

if (repeatDaily) {
        am.setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, AlarmManager.INTERVAL_DAY, sender);
    } else {
        am.set(AlarmManager.RTC_WAKEUP, triggerTime, sender);
    }

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.

if (currentHour != alarmHour && currentMin != alarmMin) {
            /*
             * If you set a repeating alarm at 11:00 in the morning and it
             * should trigger every morning at 08:00 o'clock, it will
             * immediately fire. E.g. Android tries to make up for the
             * 'forgotten' reminder for that day. Therefore we ignore the event
             * if Android tries to 'catch up'.
             */
            Log.d(LocalNotification.PLUGIN_NAME, "AlarmReceiver, ignoring alarm since it is due");
            return;
        }

To set the date, you use the date param. In your sample you're using new 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.

function onDeviceReady(){
   ...
   //note that this will return true if there is anything stored on "isAlarmSet"
   var isSet = Boolean(window.localStorage.getItem("isAlarmSet")); 
   if (isSet){
       //Alarm is not set, so we set it here
       window.localStorage.setItem("isAlarmSet",1);
    }
}

And just make sure to clear the variable if you ever unset your alarm:

localStorage.removeItem("isAlarmSet);
caiocpricci2
  • 7,714
  • 10
  • 56
  • 88
  • Thanks for the response. I have edited my code as per your suggestion. Now I have coded it in such a way that the notification gets repeated every morning at 7 AM. I tried it in my friend's phone. It works for the first time but gets repeated in the same day whenever I open my app. What might be the possible reason behind this ? – bala Jul 02 '13 at 16:55
  • The possible reason beside this might be you're calling the code every time the app runs. You're supposed to call it just once to set it and never again. – caiocpricci2 Jul 02 '13 at 19:48
  • In order to schedule it, I need to call the function following the 'ondeviceready' event. That's what I have done and it seems fine. The alarm went off at 7 AM today. Maybe the problem I mentioned earlier was just a one off thing. Thanks man !! – bala Jul 03 '13 at 13:35
  • The thing is you set on device ready, but on device ready runs once every time you open the app. That's not what you want. You want it to run only the very first time you open the app! – caiocpricci2 Jul 03 '13 at 13:47
  • Yeah but I searched for it but to no avail. Few others have also implemented it inside the deviceready callback. – bala Jul 03 '13 at 13:54
  • If it works it's fine but I still think you should not let that snippet run more than once. – caiocpricci2 Jul 03 '13 at 14:02
  • I totally agree with you. I am working on it and will definitely let you know if i manage to get it done. – bala Jul 03 '13 at 17:57
  • @gameower Exactly what I was looking for. That will save a lot of time. Will give it a try and let you know. Thanks a ton. – bala Jul 04 '13 at 18:00
  • @caiocpricci2 When I click on the notification, the app opens but the notification is not being cleared. Instead , the ticker text is coming again. This happens only between 7 AM to 7:59 AM as I have set the alarm to go off at 7 AM every morning. Any idea why ?? – bala Jul 15 '13 at 15:32
  • Are you running `notification.add()` every time you run the app? It should run only the very first time you open the app, not every time! – caiocpricci2 Jul 15 '13 at 15:59