UILocalNotification本地通知的使用方法
2013-03-12 14:57
369 查看
UILocalNotification一般做为定时器使用,可以定时提醒,定时唤醒。
1.对象的创建
2.对象的管理
3.提醒的处理,提醒触发时会回调appdelegate的如下方法
1.对象的创建
UILocalNotification *notification = [[UILocalNotification alloc] init]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSString *noticeDate = @"2012-07-30 12:00:00"; formatter.dateFormat = @"yyyy-MM-dd HH:mm"; //设置触发时间 notification.fireDate = [formatter dateFromString:noticeDate]; //触发频率,0表示只有一次,频率不能为自定义的 notification.repeatInterval = 0; notification.repeatInterval = kCFCalendarUnitDay; notification.repeatInterval = kCFCalendarUnitWeek; //用户信息,为字典类型,内容可以自己设定,这里存放了唯一的key做为标记 NSDictionary *dict = [[NSDictionary alloc] dictionaryWithObjectsAndKeys: [NSString stringWithFormat:@"%d", noticeInfo.notifyID],KEY,nil]; [notification setUserInfo:dict]; //提醒内容 notification.alertBody = @"HELLO"; //提醒铃音必须为默认的或声音文件 notification.soundName = UILocalNotificationDefaultSoundName;//@"ping.caf"; //添加到系统管理队列中 [[UIApplication sharedApplication] scheduleLocalNotification:notification];
2.对象的管理
//获取当前所有提醒,内容为UILocalNotification数组 [[UIApplication sharedApplication] scheduledLocalNotifications]; //添加提醒 UIApplication *app = [UIApplication sharedApplication]; [app scheduleLocalNotification:notification]; //取消提醒,遍历所有提醒,从中找到需要取消的那个 NSArray *myArray=[[UIApplication sharedApplication] scheduledLocalNotifications]; for (UILocalNotification *myUILocalNotification in myArray) { if ([[[myUILocalNotification userInfo] objectForKey:KEY] intValue] == value) { [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; return; } } //取消所有提醒 UIApplication *app = [UIApplication sharedApplication]; [app cancelAllLocalNotifications];
3.提醒的处理,提醒触发时会回调appdelegate的如下方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { //本地通知 NSLog(@"notification--->>%@",notification); NSString *alertBody=notification.alertBody; UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"定时提醒" message:alertBody delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }
相关文章推荐
- UILocalNotification本地通知的使用方法
- UILocalNotification本地通知的使用方法
- 本地通知 UILocalNotification的使用
- UILocalNotification的使用——本地通知
- 使用UILocalNotification给App添加本地消息通知
- 使用本地通知UILocalNotification实现简易闹钟
- 使用UILocalNotification 添加本地通知事件
- iOS UILocalNotification本地通知
- UILocalNotification实现本地的闹钟提醒的方法。
- ios本地通知UILocalNotification和远程通知UINotification
- UILocalNotification 本地通知
- UILocalNotification 本地通知的应用实例
- ios推送:本地通知UILocalNotification
- 本地通知UILocalNotification
- ios推送:本地通知UILocalNotification
- iOS UILocalNotification本地通知
- iOS开发之本地通知UILocalNotification
- UILocalNotification本地通知02
- UILocalNotification 本地通知的用法
- 【iOS开发】UILocalNotification 本地通知的实现 —— 伊禾媛