UILocalNotification
1 第一步:接收本地推送 2 3 实现代理方法didReceiveLocalNotification 4 5 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{ 6 7 //在此时设置解析notification,并展示提示视图 8 9 } 10 11 第二步:创建本地推送 12 - (void)createLocalNotification { 13 14 // 创建一个本地推送 15 16 17 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 18 19 20 21 //设置10秒之后 22 23 24 25 NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; 26 27 if (notification != nil) { 28 29 // 设置推送时间 30 31 notification.fireDate = pushDate; 32 33 // 设置时区 34 35 notification.timeZone = [NSTimeZone defaultTimeZone]; 36 37 // 设置重复间隔 38 39 notification.repeatInterval = kCFCalendarUnitDay; 40 // 推送声音 41 notification.soundName = UILocalNotificationDefaultSoundName; 42 // 推送内容 43 notification.alertBody = @"推送内容"; 44 //显示在icon上的红色圈中的数子 45 notification.applicationIconBadgeNumber = 1; 46 //设置userinfo 方便在之后需要撤销的时候使用 47 48 NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"]; 49 50 notification.userInfo = info; 51 52 //添加推送到UIApplication 53 54 UIApplication *app = [UIApplication sharedApplication]; 55 56 [app scheduleLocalNotification:notification]; 57 58 } 59 60 } 61 62 第三步:解除本地推送 63 64 - (void) removeLocalNotication { 65 66 // 获得 UIApplication 67 UIApplication *app = [UIApplication sharedApplication]; 68 69 //获取本地推送数组 70 NSArray *localArray = [app scheduledLocalNotifications]; 71 72 73 74 //声明本地通知对象 75 UILocalNotification *localNotification; 76 77 if (localArray) { 78 79 for (UILocalNotification *noti in localArray) { 80 81 NSDictionary *dict = noti.userInfo; 82 83 if (dict) { 84 85 NSString *inKey = [dict objectForKey:@"key"]; 86 87 if ([inKey isEqualToString:@"对应的key值"]) { 88 89 if (localNotification){ 90 91 [localNotification release]; 92 93 localNotification = nil; 94 } 95 96 localNotification = [noti retain]; 97 98 break; 99 100 } 101 102 } 103 104 } 105 106 107 //判断是否找到已经存在的相同key的推送 108 if (!localNotification) { 109 110 //不存在初始化 111 localNotification = [[UILocalNotification alloc] init]; 112 113 } 114 if (localNotification) { 115 //不推送 取消推送 116 [app cancelLocalNotification:localNotification]; 117 [localNotification release]; 118 119 return; 120 } 121 122 } 123 124 125 }