Local Notification repeated every day in PhoneGap Android

折月煮酒 提交于 2019-12-04 06:59:13

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!