IOS 本地通知 UILocalNotification
iOS的本地通知是用到了OC的UILocalNotification对象,加入一个通知很简单,创建一个UILocalNotification对象设置一些对应的参数就行了。
// 添加本地通知
1 //增加本地推送 2 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10]; 3 UILocalNotification *noti = [[UILocalNotification alloc] init]; 4 if (noti) { 5 //设置时间 6 noti.fireDate = date; 7 //设置时区 8 noti.timeZone = [NSTimeZone defaultTimeZone]; 9 //设置重复间隔 10 noti.repeatInterval = NSWeekdayCalendarUnit; 11 //推送声音 12 noti.soundName = UILocalNotificationDefaultSoundName;//添加声音片段,如@"beijing.caf" 13 //内容 14 noti.alertBody = @"推送内容"; 15 //现实在icon上的红色圈中的数字 16 noti.applicationIconBadgeNumber = [[[UIApplication sharedApplication] scheduledLocalNotifications] count ] + 1; 17 //添加userInfo,方便撤销时使用 18 NSDictionary *infoDic = [[NSDictionary alloc] initWithObjectsAndKeys:@"name",@"key", nil]; 19 noti.userInfo = infoDic; 20 //添加推送到UIapplication 21 [[UIApplication sharedApplication] scheduleLocalNotification:noti]; 22 }
在非ARC下,创建了就要学会释放。如果不加这一句,通知到时间了,发现顶部通知栏提示的地方有了,然后你通过通知栏进去,然后你发现通知栏里边还有这个提示,除非你手动清除
[notification release];
}
//删除、修改通知
1 - (void)applicationDidBecomeActive:(UIApplication *)application 2 { 3 application.applicationIconBadgeNumber=0; 4 int count =[[[UIApplication sharedApplication] scheduledLocalNotifications] count]; 5 if(count>0) 6 { 7 NSMutableArray *newarry= [NSMutableArray arrayWithCapacity:0]; 8 for (int i=0; i<count; i++) 9 { 10 UILocalNotification *notif=[[[UIApplication sharedApplication] scheduledLocalNotifications] objectAtIndex:i]; 11 notif.applicationIconBadgeNumber=i+1; 12 [newarry addObject:notif]; 13 } 14 [[UIApplication sharedApplication] cancelAllLocalNotifications]; 15 if (newarry.count>0) 16 { 17 for (int i=0; i<newarry.count; i++) 18 { 19 UILocalNotification *notif = [newarry objectAtIndex:i]; 20 [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 21 } 22 } 23 } 24 }
1 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 2 { 3 UIAlertView *view = [[UIAlertView alloc] initWithTitle:@"接受到本地的通知" message:notification.alertBody delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; 4 [view show]; 5 application.applicationIconBadgeNumber -= 1; 6 7 //3、取消一个本地推送 8 UIApplication *app = [UIApplication sharedApplication]; 9 //获取本地推送数组 10 NSArray *localArr = [app scheduledLocalNotifications]; 11 12 //声明本地通知对象 13 UILocalNotification *localNoti; 14 15 if (localArr) { 16 for (UILocalNotification *noti in localArr) { 17 NSDictionary *dict = noti.userInfo; 18 if (dict) { 19 NSString *inKey = [dict objectForKey:@"key"]; 20 if ([inKey isEqualToString:@"key"]) { 21 if (localNoti){ 22 localNoti = nil; 23 } 24 break; 25 } 26 } 27 } 28 29 //判断是否找到已经存在的相同key的推送 30 if (!localNoti) { 31 //不存在 初始化 32 localNoti = [[UILocalNotification alloc] init]; 33 } 34 35 if (localNoti) { 36 //不推送 取消推送 37 [app cancelLocalNotification:localNoti]; 38 return; 39 } 40 } 41 }
还有一个问题,本地通知是系统给缓存的,不管应用程序是否在前后台运行,通知都会存在。但我们把应用程序关了再重新运行时,按应用的逻辑,有些通知可能会重复添加,这不是我们想要的结果。所以,比较简单的办法是在程序启动的时候清空所有的本地通知,然后按应用逻辑判断再次重新添加。这个在程序首次起动回调函didFinishLaunchingWithOptions中加一个清空通知和重设应用提示数字了
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// about notification
application.applicationIconBadgeNumber = 0;
[[UIApplication sharedApplication] cancelAllLocalNotifications];
return YES;
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。