Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local Notification repeated every day in PhoneGap Android

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

like image 822
bala Avatar asked Oct 04 '22 19:10

bala


1 Answers

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);
like image 146
caiocpricci2 Avatar answered Oct 12 '22 01:10

caiocpricci2