iOS中添加/删除本地通知(闹钟)的方法
iOS系统默认可以向程序中增加最多64个本地通知,首先要判断机器是不是支持本地通知,然后在为每一个通知指定一个标识,用于取消时。
//添加本地通知的方法 /* message:显示的内容 firedate:闹钟的时间 alarmKey:闹钟的ID */ +(void)addLocalNotificationWithMessage:(NSString *)message FireDate:(NSDate *) fireDate AlarmKey:(NSString *)alarmKey { UILocalNotification *notification=[[UILocalNotification alloc] init]; if (notification!=nil) { notification.fireDate=fireDate; notification.timeZone=[NSTimeZone defaultTimeZone]; notification.soundName= UILocalNotificationDefaultSoundName; notification.alertBody=message; notification.hasAction = NO; notification.userInfo=[[NSDictionary alloc] initWithObjectsAndKeys:alarmKey,@"AlarmKey", nil]; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; } [notification release]; }
/* 删除本地通知 */ +(void)deleteLocalNotification:(NSString *) alarmKey { NSArray * allLocalNotification=[[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification * localNotification in allLocalNotification) { NSString * alarmValue=[localNotification.userInfo objectForKey:@"AlarmKey"]; if ([alarmKey isEqualToString:alarmValue]) { [[UIApplication sharedApplication] cancelLocalNotification:localNotification]; } } }