Create a daily local notification that will start tomorrow - ios

I have an app that reminds user about a todo item every day but I do not not need to remind them if they already did it.
My approach to solve the problem is by creating a local notification that is fired up in a daily basis at a random time. When a todo item is already done for the day, I re-scheduled the local notification day to tomorrow by cancelling it and creating a new one dated for tomorrow. But this solution doesn't work. It seems like when you specify that a local notification is fired daily (repearInterval=NSDayCalendarUnit), it doesn't honour the fireDate day property.
Here is my code:
/*** notification is created like this ***/
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [self randomHourMinFromDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"A todo item is due!";
localNotif.alertAction = #"View Item";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;
/*** some other part of the code ***/
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notification = [scheduledNotifications firstObject];
// adds 1 day to notification fireDate
NSDate *newFireDate = [notification.fireData dateByAddingTimeInterval:60*60*24*1];
[[UIApplication sharedApplication] cancelLocalNotification:notification];
notification.fireDate = newFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Can you please tell me what I'm doing wrong? Or perhaps recommend a better solution? TIA!

I have a idea. When a todo item is already done for the day, cancel the notification and create a new one. Set firedate to the current date with same time, and set dateByAddingTimeInterval: 60*60*24.
localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:60*60*24];
May this could work.

Related

UILocalNotification launched every minute util user click it

If I set my notification like the code below, it would launch alarm every minute. However, it gets triggered even my alarm is set behind the current time. For example, the current time is 12:00 and the alarm time is 9:00, it still gets triggered because kCFCalendarUnitMinute is a loop.
How can I get the function as my title description?
UILocalNotification *localNotification = [UILocalNotification new];
localNotification.userInfo = #{#"status":#"alarm",#"note":noteString,#"index":#(index)};
localNotification.fireDate = dateToFire;
localNotification.alertTitle = #"Alarm";
localNotification.alertBody = #"Wake Up!";
localNotification.soundName = #"myTone.m4a";
localNotification.alertAction = #"View";
localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.regionTriggersOnce = true;
localNotification.applicationIconBadgeNumber =
[UIApplication sharedApplication].applicationIconBadgeNumber + 1;
localNotification.timeZone = [NSTimeZone systemTimeZone];
// To set the local notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

UILocalNotification every 15 Minutes, but not at night (after 10pm)

I want to fire an UILocalNotification every 15 minutes. That is no problem:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [[NSDate date] dateByAddingTimeInterval:15];
notification.repeatInterval = NSMinuteCalendarUnit;
notification.alertBody = NSLocalizedString(#"Alert", #"");
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
But I would like to do that only between 8am and 10pm. How can I handle that?
You will have to use repeaInterval, you probably think of repeat the notification every 15 min, but you have a problem if you want to skip hours.
You could also schedule all notifications for one day (add one for 7:00am, 7:15am, 7:30am, etc) and repeat them for an interval of 1 day. If you want 4 notifications per hour, from 7am to 10pm that makes 60 notifications (which is under the max 64).

Using Textbox for local notifications

I'm trying to make it so a user can type in the text box what they need to be reminded of for a alarm to go off but if they type it in the notification doesn't show that reminder how do i fix this
You have to setup correctly your notification. Here an example with objective-c where I send a notification after 10 second.
- (IBAction)createNotification:(id)sender {
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody = #"Put Here your reminder text";
localNotification.category = #"myCategory";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
The alertBody is the text you can customize and can be the text from a textbox or another control. I hope this can help you

iOS - NSLocalNotification- set notification every other day

I want to set a Local Notification every other day (multiplication on NSDayCalendarUnit), How can I achieve this?
So far, I've done this:
- (void)scheduleLocalNotication {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
// Set the fire date/time
[localNotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
localNotification.applicationIconBadgeNumber = 1;
// Setup alert notification
[localNotification setAlertAction:#"Open App"];
[localNotification setAlertBody:#"setAreltBody"];
[localNotification setAlertBody:#"You had set a Local Notification on this time"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[localNotification setHasAction:YES];
localNotification.repeatInterval = NSDayCalendarUnit;
UIApplication *app = [UIApplication sharedApplication];
[app scheduleLocalNotification:localNotification];
}
Can I change this line:
localNotification.repeatInterval = NSDayCalendarUnit;
to something like this:
localNotification.repeatInterval = NSDayCalendarUnit * 2;
I guess not, since NSDayCalendarUnit is a typedef, so how can I do this?
I don't think this is possible using the repeatInterval property of UILocalNotification, for the same reason that you already pointed out, NSCalendarUnit is an enum, so you can only use the following values:
An alternative option would be to schedule a number of notifications in a loop and always add 24 hours to the previous fire date.

UILocalNotification end date

I wonder if it's possible to set end date for UILocalNotification?
I want my notification to fire everyday (NSDayCalendarUnit) but I have end date which I cannot cross (deadline) e.g. I'm taking a photo of my growing mustache everyday for one year period and after a year notifications won't be displayed.
I hope you got my point of view...
There is not such option in UILocalNotification as you can read in the documentation.
Your only option is to check wether the year is over when every the user starts the app.
In the UILocalNotification object, I'd recommend setting the repeatInterval property and putting the end date in the userInfo dictionary for querying later to determine if the notification has expired. For example:
UILocalNotification* uiLocalNotification;
uiLocalNotification = [[UILocalNotification alloc] init];
//Set the repeat interval
uiLocalNotification.repeatInterval = NSCalendarUnitDay;
NSDate* fireDate = ...
//Set the fire date
uiLocalNotification.fireDate = fireDate;
//Set the end date
NSDate* endDate = ...
uiLocalNotification.userInfo = #{
#"endDate": endDate
};
UIApplication* application = [UIApplication sharedApplication];
[application scheduleLocalNotification:uiLocalNotification];
//...
//Somewhere else in your codebase, query for the expiration and cancel, if necessary
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = application.scheduledLocalNotifications;
NSDate* today = [NSDate date];
for (UILocalNotification* notification in scheduledNotifications) {
NSDate* endDate = notification.userInfo[#"endDate"];
if ([today earlierDate:endDate] == endDate) {
//Cancel the notification
[application cancelLocalNotification:notification];
}
}
Use following code:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSDayCalendarUnit;

Resources