UILocalNotification
2016-01-30 17:45
405 查看
UILocalNotification本地通知,通知有两种形式,一种是网络通知一种是本地通知,本文章介绍的是本地通知常用属性的使用及我所知的注意事项
@property (strong,nonatomic) UILocalNotification *localNotification;
//初始化本地通知对象
self.localNotification = [[UILocalNotification alloc] init];
if(self.localNotification){
//获取当前时间
NSDate *now = [[NSDate alloc] init];
//设置时区(设置后提示的时间根据时区的改变而改变,若为nil将在某个时间段后发送通知,不会根据确切的时间进行通知)
self.localNotification.timeZone = [NSTimeZone defaultTimeZone];
//指定通知的提醒时间,单位是秒
self.localNotification.fireDate = [now dateByAddingTimeInterval:10];
//提示消息(桌面提示的消息,只有在锁屏或者是应用没有显示出来的时候才会显示),如果没有设置将会通知不会有提醒
self.localNotification.alertBody = @"弹出提示内容主体";
//事件提示
self.lovalNotification.alertAction = NSLocalizedString(@"起床了",nil);
//控制事件提示是否显示,默认值是YES
self.localNotification.hasAction = NO;
//设置通知提示音,这里使用的是默认的消息提示音,可以定义提示音,通知最长的提示时间是30秒,这个值无法改变,但是可以通过提示音的长短来改变提示的时长,自定以设置声音名称加上声音类型的后缀即可
self.localNotification.soundName = UILocalNotificationDefaultSoundName;
//通知的次数,就是显示在ICon左上角的圆形红色背景白色数字
self.localNotification.applicationIconBadgeNumber = 0;
//主要作用有标识本地通知、页面之间的传值携带者,数值比较多的情况下可以将数据封装在这里面进行传递
NSMuatbleDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:@"localNotificationValue" forKey:@"localNotificationKey"];
self.localNotification.userInfo = userInfo;
//重复的时间间隔,设置通知最小重复间隔是分钟,但是在枚举数据类型中有秒,但是设置了也是无效的
self.localNotification.repeatInterval = NSCalendarUnitEra;
}
//上半部分对本地通知UILocalNotification的常用设置已经基本介绍完了,本地通知在iOS下NotificationManager统一管理,上面已经将本地通知封装好了现在将封装好的本地通知对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification。
//添加本地通知有两种,一种是计划通知(最多64个通知,新的会迭代老的通知),一种是马上通知
//计划通知
[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
//马上通知
//[[UIApplication sharedApplication] presentLocalNotification:self.localNotification];
//本地通知不会因为软件的卸载就被取消了,当程序再次安装新老通知同样会存在,取消通知只能通过手动的方式进行取消
//一次性取消所有的通知,无论是计划的还是马上的都会被取消
[[UIApplication sharedApplication] cancelAllLocalNotification];
//通知触发后在AppDelegate中的代理函数,可以在这个函数里处理通知触发后的逻辑业务
//本地通知代理方法(在程序处于运行的状态下才会执行该方法)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"应用程序接受本地通知");
//取消某个特定的本地通知
for (UILocalNotification *noti
in [[UIApplication
sharedApplication] scheduledLocalNotifications]) {
NSString *notiID = noti.userInfo[@"localNotificationKey"];
NSString *receiveNotiID = notification.userInfo[@"localNotificationKey"];
if ([notiID
isEqualToString:receiveNotiID]) {
[[UIApplication
sharedApplication] cancelLocalNotification:noti];
return;
}
}
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"你好测试本地通知"
message:@"本地通知哦"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alert show];
}
本地通知就介绍到这里,后面还有通知中心的介绍
@property (strong,nonatomic) UILocalNotification *localNotification;
//初始化本地通知对象
self.localNotification = [[UILocalNotification alloc] init];
if(self.localNotification){
//获取当前时间
NSDate *now = [[NSDate alloc] init];
//设置时区(设置后提示的时间根据时区的改变而改变,若为nil将在某个时间段后发送通知,不会根据确切的时间进行通知)
self.localNotification.timeZone = [NSTimeZone defaultTimeZone];
//指定通知的提醒时间,单位是秒
self.localNotification.fireDate = [now dateByAddingTimeInterval:10];
//提示消息(桌面提示的消息,只有在锁屏或者是应用没有显示出来的时候才会显示),如果没有设置将会通知不会有提醒
self.localNotification.alertBody = @"弹出提示内容主体";
//事件提示
self.lovalNotification.alertAction = NSLocalizedString(@"起床了",nil);
//控制事件提示是否显示,默认值是YES
self.localNotification.hasAction = NO;
//设置通知提示音,这里使用的是默认的消息提示音,可以定义提示音,通知最长的提示时间是30秒,这个值无法改变,但是可以通过提示音的长短来改变提示的时长,自定以设置声音名称加上声音类型的后缀即可
self.localNotification.soundName = UILocalNotificationDefaultSoundName;
//通知的次数,就是显示在ICon左上角的圆形红色背景白色数字
self.localNotification.applicationIconBadgeNumber = 0;
//主要作用有标识本地通知、页面之间的传值携带者,数值比较多的情况下可以将数据封装在这里面进行传递
NSMuatbleDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:@"localNotificationValue" forKey:@"localNotificationKey"];
self.localNotification.userInfo = userInfo;
//重复的时间间隔,设置通知最小重复间隔是分钟,但是在枚举数据类型中有秒,但是设置了也是无效的
self.localNotification.repeatInterval = NSCalendarUnitEra;
}
//上半部分对本地通知UILocalNotification的常用设置已经基本介绍完了,本地通知在iOS下NotificationManager统一管理,上面已经将本地通知封装好了现在将封装好的本地通知对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification。
//添加本地通知有两种,一种是计划通知(最多64个通知,新的会迭代老的通知),一种是马上通知
//计划通知
[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
//马上通知
//[[UIApplication sharedApplication] presentLocalNotification:self.localNotification];
//本地通知不会因为软件的卸载就被取消了,当程序再次安装新老通知同样会存在,取消通知只能通过手动的方式进行取消
//一次性取消所有的通知,无论是计划的还是马上的都会被取消
[[UIApplication sharedApplication] cancelAllLocalNotification];
//通知触发后在AppDelegate中的代理函数,可以在这个函数里处理通知触发后的逻辑业务
//本地通知代理方法(在程序处于运行的状态下才会执行该方法)
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"应用程序接受本地通知");
//取消某个特定的本地通知
for (UILocalNotification *noti
in [[UIApplication
sharedApplication] scheduledLocalNotifications]) {
NSString *notiID = noti.userInfo[@"localNotificationKey"];
NSString *receiveNotiID = notification.userInfo[@"localNotificationKey"];
if ([notiID
isEqualToString:receiveNotiID]) {
[[UIApplication
sharedApplication] cancelLocalNotification:noti];
return;
}
}
UIAlertView *alert = [[UIAlertView
alloc] initWithTitle:@"你好测试本地通知"
message:@"本地通知哦"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil,
nil];
[alert show];
}
本地通知就介绍到这里,后面还有通知中心的介绍
相关文章推荐
- iOS 自定义刷新控件UIScrollView (Refresh)
- Codeforces 618B Guess the Permutation
- pat 1017queueing in bank (优先队列)
- [心得] 如何利用liquibase進行資料庫版本控制 - 實際練習
- String、StringBuffer、StringBuilder知识
- LinearLayout,RelativeLayout和UI优化的总结
- 工作队列(workqueue)
- smp boot up sequence
- Guideline--LM指南------------LeapMotion
- Java之GUI编程(一)
- UIImage渲染颜色
- [心得] 如何利用liquibase進行資料庫版本控制 - 基礎觀念
- priority_queue(优先队列)用法总结
- MySQL优化之慢日志分析(Anemometer+Pt-query-digest)
- Message Queue - MQ Intro
- jsp中request的常见使用方法
- UIGestureRecognizer手势
- PHP获取request payload(在header中)的方法
- LeetCode 303 Range Sum Query - Immutable(范围总和查询-永久不变)(*)
- [iOS]UICollectionView循环滚动功能的实现思路