iOS学习笔记45—本地通知UILocalNotification
来源:互联网 发布:造价通软件下载 编辑:程序博客网 时间:2024/05/08 11:08
在iOS中有两类信息提示推送方式,一类是远程服务器推送(APNS),之前有笔记中介绍过APNS:http://blog.csdn.net/tangren03/article/details/8482259,还有一类就是本地通知UILocalNotification,今天就简要的记录一下UILocalNotification的使用,代码里见注释
- UILocalNotification *notification = [[UILocalNotification alloc] init];
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- [formatter setDateFormat:@"HH:mm:ss"];
- //触发通知的时间
- NSDate *now = [formatter dateFromString:@"15:00:00"];
- notification.fireDate = now;
- //时区
- notification.timeZone = [NSTimeZone defaultTimeZone];
- //通知重复提示的单位,可以是天、周、月
- notification.repeatInterval = NSDayCalendarUnit;
- //通知内容
- notification.alertBody = @"这是一个新的通知";
- //通知被触发时播放的声音
- notification.soundName = UILocalNotificationDefaultSoundName;
- //执行通知注册
- [[UIApplication sharedApplication] scheduleLocalNotification:notification];
以上代码实现了这么一个场景:一些Todo和闹钟类应用都有通知用户的功能,使用的就是iOS中的本地通知UILocalNotification,还有些应用会在每天、每周、每月固定时间提示用户回到应用看看,也是用的本地通知,以上代码片段就是实现了在每天的下午3点弹出通知提示。
如果要在通知中携带参数信息,可以使用下面的方式:
- NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
- notification.userInfo = dic;
如果软件是在运行中,则可以通过AppDelegate中的回调方法获取并处理参数信息:
- -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
- {
- if (notification) {
- NSDictionary *userInfo = notification.userInfo;
- NSString *obj = [userInfo objectForKey:@"key"];
- NSLog(@"%@",obj);
- }
- }
另外,可以通过两种方式取消注册的本地通知,一种是取消指定的通知,第二种是取消所有的注册通知:
- [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
- [[UIApplication sharedApplication] cancelAllLocalNotification];
以上就简要介绍了一下UILocalNotification的使用,欢迎大家指正和补充!、
出在:http://blog.csdn.net/ryantang03/article/details/8914208
0 0
- iOS学习笔记45—本地通知UILocalNotification
- iOS学习笔记45—本地通知UILocalNotification
- iOS学习笔记45—本地通知UILocalNotification
- 【iOS学习笔记】本地通知 UILocalNotification
- iOS学习笔记11—本地通知UILocalNotification
- iOS学习笔记32—本地通知UILocalNotification
- iOS本地通知 UILocalNotification 笔记
- iOS本地通知UILocalNotification
- IOS 本地通知 UILocalNotification
- ios本地通知UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知:UILocalNotification
- iOS本地通知:UILocalNotification
- iOS UILocalNotification本地通知
- iOS本地通知 UILocalNotification
- 浏览器 内核与基本特性
- 【三水哥】Subset Sum = 0
- 我要学好C语言!!!
- iOS内存管理策略和实践
- 简单的理解BP神经网络
- iOS学习笔记45—本地通知UILocalNotification
- leetcode:Subsets
- informatica 代码页(codepage)字符集 UFF-8转GB2312
- iOS中百度的火星坐标和gps坐标的互相转换
- 关于JDK、JRE、JVM
- iOS开发多线程篇—NSOperation基本操作
- 剑指offer--重建二叉树
- java中泛型
- VC check box用法