- (IBAction) scheduleAlarm:(id) sender { [eventText resignFirstResponder]; // Get the current date NSDate *pickerDate = [NSDate date]; // Break the date up into components NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:pickerDate]; NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit ) fromDate:pickerDate]; // Set up the fire time NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:[dateComponents day]]; [dateComps setMonth:[dateComponents month]]; [dateComps setYear:[dateComponents year]]; [dateComps setHour:[timeComponents hour]]; // Notification will fire in one minute [dateComps setMinute:[timeComponents minute]]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; [dateComps release]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate;// this sets when notification will be called. // Notification details localNotif.alertBody = [eventText text];// this shows the alert box with message. // Set the action button localNotif.alertAction = @"View"; localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 1; // Specify custom data for the notification NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"]; localNotif.userInfo = infoDict; // Schedule the notification [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release]; }In this example we set the notification to the text that the user entered into the text field. InfoDict dictionary is our chance to associate some additional information with the alert. When a local notification is triggered, the option is available to play a sound to gain the user’s attention. If such an audio alert is required the corresponding sound file must be added to the application project resources and must be in Linear PCM, MA4 (IMA/ADPCM), uLaw or aLaw format. If no sound file is specified, the default is for the notification to be silent (though the iPhone device will still vibrate). Handling Notifications After They Fire The last thing is to determine what to do when a notification fires. This step is handled inside of the appDelegate. When a notification fires, there are one of two situations. 1. The app is running and 2. The app is not running (or running in the “background”) . Open up your app delegate .m file and add the following code.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. [window addSubview:viewController.view]; [window makeKeyAndVisible]; // Handle launching from a notification UILocalNotification *localNotif =[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotif) { NSLog(@"Recieved Notification %@",localNotif); return YES; } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { // shows badge value on tab bar NSString *badgeValue = [NSString stringWithFormat:@"%d",notifyCount]; [[[[[self tabBarontroller] tabBar] items] objectAtIndex:2] setBadgeValue:badgeValue]; // setting badge value of tab bar as shown below. application.applicationIconBadgeNumber = 0; }
The method is invoked when a running application receives a local notification. In this method, we create a string named badgeValue,which contains an integer value from notifyCount variable. We set this badgeValue in the tab bar item which is present at index no 02, which is meetings tab in the provided screenshot.


Join Us