iOS中 本地通知/本地通知详解 韩俊强的博客
布局如下:(重点讲本地通知)
iOS开发者交流QQ群: 446310206
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。
本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、 alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、 soundName和userInfo。
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
1.首先要明白模拟器和真机的区别:模拟器不会有音频提示,另外就是没有检测允许接受通知,所以我补充一下几点:
1.添加监测通知:
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];}
上代码:
#import "ViewController.h"
#import "DetailViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *schedule;
@property (weak, nonatomic) IBOutlet UIButton *unSchedule;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
}// 调度通知
- (IBAction)schedule:(UIButton *)sender {// 1.创建通知UILocalNotification *ln = [[UILocalNotification alloc]init];if (ln) {// 设置时区ln.timeZone = [NSTimeZone defaultTimeZone];// 通知第一次发出的时间ln.fireDate = [[NSDate date]dateByAddingTimeInterval:5];// 2.设置通知属性ln.soundName = @"click.wav"; // 音效文件名// 通知的具体内容ln.alertBody = @"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!....";// 锁屏界面显示的小标题,完整标题:(“滑动来”+小标题)ln.alertAction = @"查看新闻吧";// 设置app图标数字ln.applicationIconBadgeNumber = 10;// 设置app的额外信息ln.userInfo = @{@"icon":@"text.png",@"title":@"重大新闻",@"time":@"2016-02-28",@"body":@"重大新闻:小韩哥的博客又更新了,赶快进来看看吧!"};// 设置重启图片ln.alertLaunchImage = @"101339g76j7j9t2zgzdvkj.jpg";// 设置重复发出通知的时间间隔
// ln.repeatInterval = NSCalendarUnitMinute;// 3.调度通知(启动任务,在规定的时间发出通知)[[UIApplication sharedApplication]scheduleLocalNotification:ln];// 直接发出通知没意义
// [[UIApplication sharedApplication]presentLocalNotificationNow:ln];}}
- (IBAction)noSchedule:(UIButton *)sender
{
// [[UIApplication sharedApplication]cancelAllLocalNotifications];// 已经发出且过期的通知会从数组里自动移除NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;NSLog(@"%@",notes);
}- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note
{DetailViewController *detailVC = segue.destinationViewController;detailVC.userInfo = note.userInfo;
}
@end
2.通知详情页面设置基本属性:
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController@property (nonatomic, strong) NSDictionary *userInfo;
@end.m
#import "DetailViewController.h"@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UILabel *userInfoContent;@end@implementation DetailViewController- (void)viewDidLoad {[super viewDidLoad];self.userInfoContent.text = self.userInfo[@"body"];
}- (void)setUserInfo:(NSDictionary *)userInfo
{_userInfo = userInfo;
}
@end
3. didFinishLaunchingWithOptions 实时监测:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//注册本地通知if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];}// NSLog(@"-----didFinishLaunchingWithOptions---");UILabel *label = [[UILabel alloc]init];label.frame = CGRectMake(0, 64, 320, 100);label.backgroundColor = [UIColor redColor];label.font = [UIFont systemFontOfSize:11];label.numberOfLines = 0;label.textColor = [UIColor whiteColor];label.text = [launchOptions description];[[[self.window.rootViewController.childViewControllers firstObject] view]addSubview:label];UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsURLKey];if (note) {label.text = @"点击本地通知启动的程序";}else{label.text = @"直接点击app图标启动的程序";}self.label = label;return YES;
}
/*** 当用户点击本地通知进入app的时候调用(app当时并没有被关闭)* 若app已关闭不会被调用此方法*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{self.label.text = @"点击通知再次回到前台";ViewController *homeVC = [self.window.rootViewController.childViewControllers firstObject];
// [homeVC performSegueWithIdentifier:@"toHome" sender:notification];[homeVC performSegueWithIdentifier:@"toHome" sender:notification];}
三种情况展示:(重要)
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
1.程序运行在后台
每日更新关注:http://weibo.com/hanjunqiang 新浪微博
Demo下载地址Github: https://github.com/XiaoHanGe/LocalNotification
iOS开发者交流QQ群: 446310206
iOS中 本地通知/本地通知详解 韩俊强的博客相关推荐
- iOS中 HTTP/Socket/TCP/IP通信协议详解 韩俊强的博客
版权声明:本文为博主原创文章,未经博主允许不得转载. 每日更新关注:http://weibo.com/hanjunqiang 新浪微博 简单介绍: [objc] view plaincopy // ...
- iOS中 最新微信支付/最全的微信支付教程详解 韩俊强的博客
亲们, 首先让我们来看一下微信支付的流程吧. 1. 注册微信开放平台,创建应用获取appid,appSecret,申请支付功能,申请成功之后会返回一些参数. 2. 下载微信支付sdk 3. 客户端请求 ...
- iOS中 语音识别功能/语音转文字教程详解 韩俊强的博客
原文地址:http://blog.csdn.net/qq_31810357/article/details/51111702 前言:最近研究了一下语音识别,从百度语音识别到讯飞语音识别:首先说一下个人 ...
- iOS中 CoreGraphics快速绘图(详解) 韩俊强的博客
第一步:先科普一下基础知识: Core Graphics是基于C的API,可以用于一切绘图操作 Core Graphics 和Quartz 2D的区别 quartz是一个通用的术语,用于描述在IOS和 ...
- iOS中 HeathKit框架学习 步数统计等 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博!iOS开发者交流QQ群: 446310206 HeathKit框架学习 本文结构 简介 用户数据安全及隐私 Heat ...
- HTML5中 HTML列表/块/布局 韩俊强的博客
从简单到复杂HTML5详解:每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 1.HTML列表 1.有序 2.无序 3.有序star属性 4.有序无序列表 代码: ...
- iOS中 Realm的学习与使用 韩俊强的博客
iOS开发者交流QQ群:446310206 有问题或技术交流可以咨询!欢迎加入! 这篇直接搬了一份官方文档过来看的 由于之前没用markdown搞的乱七八糟的 所以重新做了一份 后面看到官网的中文文 ...
- iOS中 流媒体播放和下载 韩俊强的博客
iOS中关于流媒体的简介:介于下载本地播放与实时流媒体之间的一种播放形式,下载本地播放必须全部将文件下载完成后才能播放,而渐进式下载不必等到全部下载完成后再播放,它可以一边下载一边播放,在完成播放内容 ...
- iOS中 Animation 动画大全 韩俊强的博客
每日更新关注:http://weibo.com/hanjunqiang 新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...
最新文章
- R与Python之间该如何选择
- Spring boot全面接管Spring MVC
- 知道你用linux可视文件系统为什么搜索不到文件吗?(隐藏文件夹搜不到!!要用find . -name “xxx“命令)
- 新闻文字上下滚动代码
- 3DSlicer17:Logics
- view.ondraw
- 定了!华为P30/P30 Pro正式官宣:3月26日见
- WebRTC与Ace在线代码编辑器合作,实现实时协作编程
- 微信小程序-滚动消息通知
- 经典排序算法(一)--快速排序Quick Sort
- python列表模糊匹配_Python下用List对员工信息表进行模糊匹配
- burpsuite插件xssValidator的安装及使用(XSS自动扫描工具)
- 熔断机制什么意思_熔断机制是什么意思?
- Springboot RestTemplate post/get请求所有情况
- Java中输入一个十进制数,如何转换为二进制数
- 浅谈SRAM与DRAM的异同
- Vue改变网页背景颜色切换
- 职高计算机应用基础教学总结,中职《计算机应用基础》教学心得
- 如何实现单片机按键长按和短按功能
- Django 多表操作
热门文章
- 《生物化学与分子生物学》----蛋白质----听课笔记(七)
- 【转贴】学SAP Basis 9 年来的一点心得
- Springboot读取.properties配置文件并取值
- 汽车链的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- Azure上部署的资源公网连通性测试工具-psping
- C# 对象、文件与二进制串(byte数组)之间的转换
- 领课教育开源系统-Redis的安装和使用
- 【java基础04:注释 关键字 标识符 字面值 字节】
- JZOJ4597. 现世斩 题解
- NOMA的FTPC(FTPA)功率分配