iOS 本地推送
1 //********************************第一步:创建本地推送********************************** 2 // 创建一个本地推送 3 UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease]; 4 //设置10秒之后 5 NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10]; 6 if (notification != nil) { 7 // 设置推送时间 8 notification.fireDate = pushDate; 9 // 设置时区 10 notification.timeZone = [NSTimeZone defaultTimeZone]; 11 // 设置重复间隔 12 notification.repeatInterval = kCFCalendarUnitDay; 13 // 推送声音 14 notification.soundName = UILocalNotificationDefaultSoundName; 15 // 推送内容 16 notification.alertBody = @"推送内容"; 17 //显示在icon上的红色圈中的数子 18 notification.applicationIconBadgeNumber = 1; 19 //设置userinfo 方便在之后需要撤销的时候使用 20 NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"]; 21 notification.userInfo = info; 22 //添加推送到UIApplication 23 UIApplication *app = [UIApplication sharedApplication]; 24 [app scheduleLocalNotification:notification]; 25 26 } 27 28 //***********************************************第二步:接收本地推送 ******************************************** 29 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{ 30 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; 31 [alert show]; 32 // 图标上的数字减1 33 application.applicationIconBadgeNumber -= 1; 34 } 35 36 //***********************************************第三步:解除本地推送********************************************* 37 // 获得 UIApplication 38 UIApplication *app = [UIApplication sharedApplication]; 39 //获取本地推送数组 40 NSArray *localArray = [app scheduledLocalNotifications]; 41 //声明本地通知对象 42 UILocalNotification *localNotification; 43 if (localArray) { 44 for (UILocalNotification *noti in localArray) { 45 NSDictionary *dict = noti.userInfo; 46 if (dict) { 47 NSString *inKey = [dict objectForKey:@"key"]; 48 if ([inKey isEqualToString:@"对应的key值"]) { 49 if (localNotification){ 50 [localNotification release]; 51 localNotification = nil; 52 } 53 localNotification = [noti retain]; 54 break; 55 } 56 } 57 } 58 59 //判断是否找到已经存在的相同key的推送 60 if (!localNotification) { 61 //不存在初始化 62 localNotification = [[UILocalNotification alloc] init]; 63 } 64 65 if (localNotification) { 66 //不推送 取消推送 67 [app cancelLocalNotification:localNotification]; 68 [localNotification release]; 69 return; 70 } 71 }
激情为梦想而生