How to set an Alarm in iOS? - 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];

Related

Error in Notification Part

I am working with the notification part of the project, in this I want the sound notification which will notify the user about the activity. The code I am working with is below:
-(void) scheduleNotificationForDate {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateParts = [[NSDateComponents alloc] init];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
NSDate *sDate = [calendar dateFromComponents:dateParts];
NSLog(#"date : %#", sDate);
localNotif.fireDate = sDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = NSLocalizedString(#"View Details", nil);
localNotif.alertBody = #"Hello Testing";
localNotif.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
NSLog(#"notification started");
}
With the help of this code I am getting the written notification on my device but I don't hear any alert sound when notification is alerted.
Please have a look on the code and try to locate my error. Your help will be much appreciable.
Look at your console.
date : 0001-01-01 17:26:32 +0000
You have scheduled the notification ~2000 years in the past, because you didn't specify components for year, month and day. Setting a fireDate that is in the past means the notification is delivered immediately.
If you want to schedule a notification for today at 18:30 you could do something like this:
// get year, month and day components from current time
NSDateComponents *dateParts = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]];
[dateParts setHour:18];
[dateParts setMinute:20];
[dateParts setSecond:00];
May be you have notification sound for this app disabled in Settings/Notification Center?
If your app is in active state it will not give alert sound, put it in background and test it.
NOTE: make sure your system volume is on .

How to set UILocalNotification for 30 consecutive days

I want to schedule UILocalNotificaion for 30 consecutive days at 8:00 AM daily and i want to implement that functionality with one UILocationNotification instance only. Here is my code to schedule local notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
It will fire daily notifications forever at 08:00AM, but i want to be notified for 30 consecutive days only. The easiest solution is to fire 30 UILocalNotifications but i want to do that with 1 UILocalNotification instance. How can i do that? Please help.
In UILocalNotification userInfo property save NSDate object when you create Notification.
When you open Notification,
check with the current date with date in userinfo . If there is difference is more than 30 days you can cancel your local notification.
NSDate *now = [NSDate date];
// now a NSDate object for now + 30 days
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:30];
NSDate *endDate = [gregorian dateByAddingComponents:offsetComponents toDate:now options:0];
After that check:
if([currentDate isEarlierDate:endDate]){
//Fire the notification
}

How to allocate custom time in UILocalnotification?

- (void)scheduleNotification :(int) rowNo
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.timeZone = [NSTimeZone defaultTimeZone];
NSString *descriptionBody=[[remedyArray objectAtIndex:rowNo]objectForKey:#"RemedyTxtDic"];
NSLog(#"%#",descriptionBody);
notif.alertBody = [NSString stringWithString:descriptionBody];
notif.alertAction = #"Show me";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:notif.alertBody
forKey:#"kRemindMeNotificationDataKey"];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
I have a column name frequency which is fetched from Sqldb where it contains the number of times notification should appear for a particular cell.
if frequency = 3 ..the notification should fire say 8 AM , 2PM then 8PM
if frequency = 4 ..the notification should fire say 8 AM , 12PM , 4PM then 8PM.
Is there a way to do it? If anyone can help me that would be great
Unfortunately, You can specify value for repeatInterval only of type NSCalendarUnit (day, week, month). So, I think, You need to create several Notifications with different fireDate, and specify for them repeatInterval = NSDayCalendarUnit
For example,
NSDate *currentTime = [NSDate date];
notification1.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL sinceDate:currentTime];
notification1.repeatInterval = NSDayCalendarUnit;
notification2.fireDate = [NSDate dateWithTimeInterval:SOME_INTERVAL * 2 sinceDate:currentTime];
notification2.repeatInterval = NSDayCalendarUnit;
After user viewed some of notifications - You can cancel them.
Update.
You can also create NSDate from different components like this:
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setWeekday:2]; // Monday
[components setWeekdayOrdinal:1]; // The first Monday in the month
[components setMonth:5]; // May
[components setYear:2013];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
Also You can set hour, minutes, seconds, timezone and other parameters.

Why local notification is repeated after 1 minute when the repeat interval is set 1 sec(NSSecCalendarUnit)?

I am trying to schedule a local notificaition that will repeat after every 1 sec once the notification is fired. The notification is fired 10 sec after the application starts.
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Did you forget something?";
notif.alertAction = #"Show me";
//notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = #"applause-light-01.wav";
notif.applicationIconBadgeNumber = 1;
notif.repeatInterval = NSSecondCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Even thought I have used notif.repeatInterval = NSSecondCalendarUnit, notification repeat after 60 sec. What is that I am doing wrong?
notif.fireDate = [[NSDate alloc]initWithTimeInterval:10 sinceDate:[NSDate date]];
This line of code makes your local notification to fire after the new date is created. If you want the notifications right away then you should just create a simple date like this,
notif.fireDate = [NSDate date];
Hope this helps.

Is the user able to disable local notifications?

I have created an app that will give the user a daily notification at 9am, but want the user to be able to turn it off if they wish.
i have set up a settings bundle for the app, and set up a slider for enableNotifications. When i build and test the app, the settings are there and I can turn them on or off...but it doesnt seem to be registering it in the app. How do I program the app to check whether or not the notification setting is on or off?
I saw this answer: Determine on iPhone if user has enabled push notifications
but I dont know where to put it in and program it properly (if/else statements, etc)
Here is the code where the notification is created:
-(id) getCommandInstance:(NSString*)className
{
/** You can catch your own commands here, if you wanted to extend the gap: protocol, or add your
* own app specific protocol to it. -jm
**/
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate:currentDate];
NSDateComponents *temp = [[NSDateComponents alloc]init];
[temp setYear:[dateComponents year]];
[temp setMonth:[dateComponents month]];
[temp setDay:[dateComponents day]];
[temp setHour: 22];
[temp setMinute:00];
NSDate *fireTime = [calender dateFromComponents:temp];
[temp release];
// set up the notifier
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = fireTime;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"Don't Forget Your 365 Day Photo Challenge!";
localNotification.alertAction = #"LAUNCH";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[localNotification release];
return [super getCommandInstance:className];
}
If you have just one localNotification and want to cancel it you can just call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
This will not throw an error if there aren't any localNotifications. You can call this through a button press IBAction. If you want to set the state of a switch based on whether there are any localNotifications then you can you can check to see it there are any:
NSArray* localNotifications = [[UIApplication sharedApplication]
scheduledLocalNotifications];
If localNotifications.count > 0 then you have some notifications scheduled.
Based on your description, I think you are asking how to retrieve the value of the notification setting from your app's preference, i.e. whether notification should be enabled or disabled.
You should use [NSUserDefaults standardUserDefaults] to access preference values. Please refer to this Accessing Preference Values document.

Resources