一,创建一个本地通知,很简单;
1.创建一个UInotification对象;
2.设置 UInotification对象的属性;
3.注册UInotification对象;
//创建UInotification对象 noti; UILocalNotification *noti=[[UILocalNotification alloc] init]; //设置UInotification对象 noti 的属性; /*fireDate 推送通知的时间 *timeZome 推送通知的市区 *soundName 推送通知的声音 *alertBody 推送通知的内容 *applicationIconBadgeNumber *userInfo 撤销推送通知的标志 */ noti.fireDate=[NSDate dateWithTimeIntervalSinceNow:20]; noti.timeZone=[NSTimeZone defaultTimeZone]; noti.repeatInterval=NSWeekdayCalendarUnit; noti.soundName=UILocalNotificationDefaultSoundName; noti.alertBody=@"本地通知的内容"; noti.applicationIconBadgeNumber=1; NSDictionary *infodic=[NSDictionary dictionaryWithObject:@"name" forKey:@"key"]; noti.userInfo=infodic; //注册 UInotification 对象; UIApplication *app=[UIApplication sharedApplication]; [app scheduleLocalNotification:noti];
二,在APP运行时显示通知;
本地通知来的时候,会调用*AppDelegate.m里面的方法
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification
所以我们只需重新 改方法,就可以在APP运行的时候也能得到本地通知;
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"接收到本地提醒 in app" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil]; [alert show]; //取消 badge; application.applicationIconBadgeNumber -= 1; // 下面你就可以处理一些你喜欢的事了; }
三。取消本地通知;
-(void)cancelNoti:(NSString*)key{ UIApplication *app=[UIApplication sharedApplication]; NSArray *notiArray=[app scheduledLocalNotifications]; UILocalNotification *noti; if (notiArray) { for (UILocalNotification *oneNote in notiArray) { NSDictionary *oneUserinfo=oneNote.userInfo; if (oneUserinfo) { NSString *detailvalue=[oneUserinfo objectForKey:@"key"]; if ([detailvalue isEqualToString:key]) { if (noti) { //[noti release]; noti=nil; } //noti=[oneNote retain]; noti=oneNote; break; } } } if (noti) { [app cancelAllLocalNotifications];//取消所有的本地通知 //[app cancelLocalNotification:noti];//取消某个通知 } } }
浙公网安备 33010602011771号