UILocalNotification end date - ios

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;

Related

Objective-C: UILocalNotification

ViewController:
- (void)viewDidLoad
{
`[super viewDidLoad];`
dateFormatter.dateFormat = #"dd/MM";
timeFormatter.dateFormat = #"HH:mm";
NSString *dateString = [dateFormatter stringFromDate:dateTime];
NSString *timeString = [timeFormatter stringFromDate:dateTime];
if ([dateString isEqual:#"23/06"]) {
if ([timeString isEqual:#"23:30"]) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}
AppDelegate:
[[UIApplication sharedApplication] scheduledLocalNotifications];
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
}
The notification isn't receiving when time equals string and date.
Please help!
The core issue with this (aside from using strings to compare dates) is that viewDidLoad gets called only once in this application. Because you are only scheduling the local notification when the current date is 11:30, it will never get called, since you also set the fireDate to the same exact time.
The thing is, local notifications get scheduled prior to the event. In fact, you should set dateTime to the desired schedule time and then set the local notification.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2016];
[dateComponents setMonth:6];
[dateComponents setDay:23];
[dateComponents setHour:23];
[dateComponents setMinute:30];
dateTime = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Based on your uploaded code, it looks like you are going to be grabbing the date from the datePicker, so eventually, you will move the UILocalNotification portion to the button method and use [datePicker date] as the fireDate. Meaning, you can ignore the dateComponents part.

Repeat Local notifications for selected days at the same time

In my app I am using UILocalNotifications. I want a functionality in which user can repeat alarms for specific days. Like(Monday,Wednesday,Sunday) at say 9pm. How can i do this.
Thanks in Advance
I am using the following code
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:alertBody];
[notification setFireDate:[myDatePicker date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = notification.applicationIconBadgeNumber+1;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
There's a repeatInterval property on the noitification , use that.
Err... ...why don't just schedule three notifications for the three days, each with a week as repreatInterval?
Try this code
NSDate *AlarmTime1 =[[NSDate date] dateByAddingTimeInterval:expiretime];
UIApplication *app1 = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm1 = [[UILocalNotification alloc] init];
if (notifyAlarm1) {
notifyAlarm1.fireDate = AlarmTime1;
notifyAlarm1.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval=0;
notifyAlarm.soundName= #"applering_zwnNYFkA.wav";
notifyAlarm1.soundName=#"stop.wav";
notifyAlarm1.alertBody =NSLocalizedString(#"EXPIRE_DESC", nil);
[app1 scheduleLocalNotification:notifyAlarm1];

Create a daily local notification that will start tomorrow

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.

I am trying to fire local notification every 5 seconds using this code

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
return;
//localNotification.fireDate = [NSDate dateWithTimeInterval:timeInterval sinceDate:now];
localNotification.fireDate = [NSDate date];
localNotification.repeatInterval = 5*NSSecondCalendarUnit;
localNotification.alertBody = #"Your alert message";
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
This code generates notification after first 5 second then it goes back to NSMinuteCalendarUnit, I have been trying to work around with this but no help.
I want to set notifications every 5 seconds and then it should fire it until i forcefully want to stop it.
Can somebody please help??
The repeatInterval of a UILocalNotification is of type NSCalendarUnit not an time interval. You can only assign a value from the NSCalendarUnit enum to it.
If you want to fire a local notification every 5 seconds you will need to set multiple notifications.
Here is the code.
Use this for every 5 minutes instead of every week.
-(void)setLocalNotificationwithOptions :(NSDictionary *)launchOptions
{
UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNoti) {
NSLog(#"Notification:%#",localNoti);
}
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSCalendar *gregCalendar12 = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent12 = [gregCalendar12 components:NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
[dateComponent12 setWeekday:7];
[dateComponent12 setHour:14];
[dateComponent12 setMinute:46];
UILocalNotification *notification12 = [[UILocalNotification alloc]init];
[notification12 setAlertBody:#"ENJOY YOUR WEEKEND!"];
[notification12 setFireDate:[gregCalendar12 dateFromComponents:dateComponent12]];
notification12.repeatInterval = NSMinuteCalendarUnit;
[notification12 setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification12];
}
Hope this helps you. Thank you..
if you want to repeat the timer every 5 seconds,
UILocalNotification allows until 30 seconds per one sound.
So put a 30 seconds sound in your directory,
and then
[[UIApplication sharedApplication] cancelAllLocalNotifications];
NSDate *date = "set your notification date"
for (int i = 0; i < 10; i++){
NSDate *repeatTime = [date dateByAddingTimeInterval:300 * i];
notification.fireDate = repeatTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

How to set an Alarm in iOS?

I know this question has asked many times on StackOverflow but i couldn't able to set alarm in my app because i am very new to iOS? I am following this tutorial to set an alarm:
Setting a reminder using UILocalNotification in iOS.
However, it doesn't seems to be working for me.
I am in need to set alarm daily lets say 5.00 PM daily. I can't use date picker for choosing the time.
First on your xib, (or code) set the date picker mode: Time (Default is date & time)
The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:
Local notifications in iOS.
tutorial you posted on how to do this.
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
Call this method with parameters and use this
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You may need to change the style of the date picker to allow changing the time in addition to just the date.
You can try this
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:#"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];

Resources