本地通知
来源:互联网 发布:autodesk公司软件 编辑:程序博客网 时间:2024/05/02 04:59
发送通知
- (IBAction) scheduleAlarm:(id) sender {[eventText resignFirstResponder];NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];// Get the current dateNSDate *pickerDate = [self.datePicker date];// Break the date up into componentsNSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:pickerDate];NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) 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]];[dateComps setSecond:[timeComponents second]]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; [dateComps release]; UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; localNotif.timeZone = [NSTimeZone defaultTimeZone];// Notification details localNotif.alertBody = [eventText text];// Set the action button localNotif.alertAction = @"View"; localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.repeatInterval = NSMinuteCalendarUnit; 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];[self.tableview reloadData];}
接受通知:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {// Handle the notificaton when the app is runningNSLog(@"Recieved Notification %@",notif); NSLog(@"%@", [notif.userInfo objectForKey:@"someKey"]);}