Invoking Alarm for certain time in ios - iphone

I am working on App which will set an alarm on ios for a time depending on user input.
Meaning: if a user selects row 1 of table then it will look into dictionary (which may say 20 minutes),,, then it should set an alarm in ios for (currrent time+ 20 minutes).
Can someone please tell me the best way to approach this.

You can use UILocalNotification:
UILocalNotification *local = [[UILocalNotification alloc] init];
// create date/time information
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:20*60]; //time in seconds
local.timeZone = [NSTimeZone defaultTimeZone];
// set notification details
local.alertBody = #"Alarm!";
local.alertAction = #"Okay!";
local.soundName = [NSString stringWithFormat:#"Default.caf"];
// Gather any custom data you need to save with the notification
NSDictionary *customInfo =
[NSDictionary dictionaryWithObject:#"ABCD1234" forKey:#"yourKey"];
local.userInfo = customInfo;
// Schedule it!
[[UIApplication sharedApplication] scheduleLocalNotification:local];
[local release];

Related

Multiple reminders in single iPhone app

how can we set more than one reminder's in our iPhone app, i have to set individual reminder's for every image projects in my app so that a user can take image (daily, weekly or monthly) from the camera for a specific project, i am able to set single reminder but when i tried to set more than one reminder for another project in my app it overwrites all the previous reminders of all projects. please give me any idea.
try this.
//KeyValue = used for identifying reminder
//RepeatType = NSWeekCalendarUnit or NSMonthCalendarUnit
//AlertBody = display text
-(void)setReminder:(NSDate*)date KeyValue:(NSString*)keyValue RepeatType:(NSInteger)repeatType AlertBody:(NSString*)alertBody
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = alertBody;
// Set the action button
localNotif.alertAction = NSLocalizedString(#"View",nil);
localNotif.soundName =UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%#",keyValue] forKey:#"ReminderID"];
localNotif.userInfo = infoDict;
localNotif.repeatInterval = repeatType;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
//call this method for setting single notification.
//you can set as many as you want

How to create Reminder for some time like Remind me in 30 mins in iPhone

Hi I working on a reminder application.
I need to display a reminder alert after some particular time.
But not at the time we have set in date picker.
Just like I have a button 'Remind in 10 Mins'
-(IBAction)ReminderClick:(id)sender
{
}
When user press the button , After 10 mins it needs to display an alert.
You need to use UILocalNotification for this Function
Code is look like
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc] init];
NSDate *date1=[fire dateByAddingTimeInterval:60];
notifyAlarm.fireDate = date1;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
//notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval =NSWeekCalendarUnit ;
notifyAlarm.soundName =soundString;
notifyAlarm.alertBody =snoozeBody;
notifyAlarm.userInfo=snoozeDict;
//notifyAlarm.alertLaunchImage=#"in.png";
[app scheduleLocalNotification:notifyAlarm];
and You can follow thos tutorial for this
http://www.icodeblog.com/tag/uilocalnotification/
http://blog.mugunthkumar.com/coding/iphone-tutorial-scheduling-local-notifications-using-a-singleton-class/
http://www.iostipsandtricks.com/ios-local-notifications-tutorial/
http://www.youtube.com/watch?v=tcVoq488-XI
You have to use UILocalNotificataion for this

How to initialize local notification?

I want to implement local notification in my clock app.Basically i want that a music file should be played after every half an hour like in ship's clock in which chimes are played after every 30 minutes.
Can anyone give rough idea as how i can implement this functionality even when the app enters in background?
I recently used the Local notification stuff and used the following functions
//Setting up the Local Notifications
for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly
NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,#"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];
[self scheduleNotificationWithItem:dataDict];
}
Where scheduleNotificationWithItem is defined as
- (void)scheduleNotificationWithItem:(NSDictionary*)item {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) return;
localNotification.fireDate = [item valueForKey:FIRE_TIME_KEY];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%#", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = item;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
Finally you can handle these notifications as
You can handle these notifications as follows
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary
}
reading the developer documentation will help you more to understand the stuff.Hope it helps
You can use UILocalNotifications and set their 'firedate', according to your requirement and then schedule the notification. These notifications doesn't bother whether your app is running or is in background they will always show up like an alertview.

iPhone sdk: how to increase the local notification count

Hi I am working on Google calendar, I have to remain user events with local notifications, when event starts.
for that I have to show notifications, if user have two events at a time then local notification count has to increase on app icon.(If I have two events at a time also count is showing only one local notification count on the app icon).
please suggest me how to increase the local notification count on the app icon.
please check my code.
//Local notifications delegates and methods.
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[when startTime] date];
notif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
notif.alertBody = titles;// here title is the key word for the event
// Set the action button
notif.alertAction = nil;
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *userDict = [NSDictionary dictionaryWithObject:titles forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
Thank you
Assuming that the variable count contains the correct number to shown on the icon's badge, you simply do the following:
[UIApplication sharedApplication].applicationIconBadgeNumber = count;
my answer will need a database:
create an array (database) sorted with date and count. When a notification fires up, call it's count and display it using
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: badgeSortCount]

UILocalNotification : updating BadgeNumber during repeatInterval

After goggling for 2 days i couldn't find any solution as if its clear to everyone (but me) !
I need the:
Alert.applicationIconBadgeNumber = x
to be updated in background each time the notification fires, I am repeating the notify by:
notif.repeatInterval = NSMinuteCalendarUnit
Repeating is working fine every 1 m. when the app goes in background, but the BadgeNumber dosent get updated, it takes the 1st updated date value only.
I am calling the scheduleNotification method by viewDidLoad
Here is my full code:
- (void)scheduleNotification {
UILocalNotification *notif;
notif = [[[UILocalNotification alloc] init] autorelease];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.fireDate = [[NSDate date] dateByAddingTimeInterval:5];
notif.repeatInterval = NSMinuteCalendarUnit;
NSInteger BadgeNumber = [self BadgeNumber];
NSInteger *BadgeNumberPointer = &BadgeNumber;
NSString *BadgeNumberString = [NSString stringWithFormat:#"%i", BadgeNumber];
notif.applicationIconBadgeNumber = *BadgeNumberPointer;
notif.alertBody = BadgeNumberString;
notif.alertAction = #"Hello";
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
-(int)BadgeNumber{
NSDate *currentDateUpdate = [[NSDate alloc] init];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat:#"dd"];
NSString *dateCheckUpdate = [formatter2 stringFromDate:currentDateUpdate];
NSInteger dateCheckUpdateInt = [[dateCheckUpdate substringWithRange:NSMakeRange(0, 2)] integerValue];
int BadgeNumber = dateCheckUpdateInt;
return BadgeNumber;
}
Kindly advice how to fix it,, thanking all of you.
Because the operating system copies the notification when scheduled, the data in the notification is not updated, therefore the application badge number doesn't change.
Maybe if you don't repeat the notification but generate your own new notification for each time interval it will give you the behavior you need. The only way I can think of generating notifications like that is to post a bunch of notifications in your scheduleNotification method, and then remember to delete the notifications when the user responds in the proper way. Since the OS only remembers the next chronologically scheduled 64 notifications, you could only schedule about an hour's worth. Since your badge number seems to be the current date, you could check the time and only bother with setting so many notifications if you're within an hour of midnight.
I don't understand what you are trying to accomplish by nagging the user so often, nor telling them the date in the badge number. Any app that bothered me so much or misused the badge number so would quickly get deleted from my iOS devices. Maybe rethinking what you are trying to accomplish may direct you to a better solution.
I know this is already answered, but you could use NSUserDefaults as a means of caching the badge count. Then in applicationIconBadgeNumber you can just use something like this:
notif.applicationIconBadgeNumber = ([NSUserDefaults standardUserDefaults] integerForKey:#"badgeCount"] + 1);
and then you could just reset it when the user responds accordingly.