Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 273
vinothp Avatar asked Aug 07 '12 16:08

vinothp


2 Answers

  1. First on your xib, (or code) set the date picker mode: Time (Default is date & time)

  2. 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.

like image 75
gsach Avatar answered Sep 19 '22 13:09

gsach


+ (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];
}
like image 22
Prabhjot Singh Gogana Avatar answered Sep 20 '22 13:09

Prabhjot Singh Gogana