大家都知道Push Notification,这个东西需要联网才可以用。iOS4起,苹果引入了一种可以在设备内部引发的notification。不需要复杂的服务器编程,或其他复杂的配置。这个技术就是Local Notification。

Local notificaton可以在用户设定里定时触发。甚至,你可以设定重发。下面,我们就来研究下Local Notification,看看如何设定,查看和处理Local Notification。

1. 创建项目

总体来说是这么个效果:

一共分三层,最上面一层是DatePicker。用于选择出发Local Notification的时间。第二层是一个TextField,用于设定Notification的title。最后是设定过的Notification都放在一个TableView里。看看都有什么:

    UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 320, 160)];
[self.view addSubview:datePicker]; UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 227, 150, 40)];
[self.view addSubview:textField];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = @"Input Text";
self.textField = textField; UIButton *saveButton = [[UIButton alloc] initWithFrame:CGRectMake(155, 227, 160, 40)];
[self.view addSubview:saveButton];
saveButton.backgroundColor = [UIColor orangeColor];
[saveButton setTitle:@" SAVE " forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside]; CGFloat height = [UIScreen mainScreen].bounds.size.height - 242;
UITableView *notiTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 268, 320, height)];
[self.view addSubview:notiTableView];
notiTableView.delegate = self;
notiTableView.dataSource = self;

与UI元素对应的,我们这里有三层,四个界面元素。同事还有Button的一个action。同事需要注意的是,我们需要在Controller里实现UITableViewDelegate和UITableViewDataSource这两个protocol。要是用UITableView这两个protocol必须实现。

其他还要做一些必要的操作。比如创建一个空的设定notification的方法,等。

#pragma mark - Private methods

- (void)saveAction:(id)sender{
DLog(@"");
[self.textField resignFirstResponder];
} #pragma mark - UITableViewDelegate & datasource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return ;
} - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} return cell;
}

在saveAction方法中,设定notification时退出textfield的编辑状态并隐藏键盘。所以,需要调用resignFirestResponder方法,[self.textField resignFirstResponder]

后面的UITableView相关的protocol中,暂时指定返回的行数为1,并在返回Cell的方法中返回没有设定任何内容的行。这样,代码就可以正常运行。否则,会因为没有实现必要的protocol方法而crash。

这个时候,在运行app之后就会出现上面给出的效果图。(注意:这里用的是真机运行的app)

2. 设定notification

这里是全教程的核心所在了。折腾完全文也都是基于这里所讲的内容。

首先,在DatePicker里设定好时间。之后在设定notification的内容。最后把指定推送在指定的时间引发。

- (void)saveAction:(id)sender{
DLog(@"");
[self.textField resignFirstResponder]; NSDate *pickerDate = [self.datePicker date]; UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification) {
localNotification.fireDate = pickerDate;//fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = self.textField.text;
localNotification.alertAction = @"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
localNotification.userInfo = @{@"userId": @"best user", @"userName": @"Jack"};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; [self.notificationTableView reloadData];
}
}

就如前文所述,使用NSDate *pickerDate = [self.datePicker date]; 获取到DatePicker中设定的日期,之后在Local notification对象的fireDate属性里赋值。localNotification.alertBody = self.textField.text设定提示的文本。使用localNotification.userInfo = @{@"userId": @"best user", @"userName": @"Jack"}设定用户信息,就是你觉得有用的信息以字典的方式存放在notification对象中,在其他的时候可以获取这个字典。最后,最关键的就是使用[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]设定notification在指定的时间触发。

到这里,就可以指定一个Local notification在给定的时间触发了。但是还有一种场景,在用户做了某个操作的时候,notification立刻触发。这时,再用schecule的方法就会很奇怪,schedule一秒可能赶不上,schedule的秒多了可能错过正确的提示时机。所以API里提供了另一个方法presentNow,提示立刻触发。正确的立刻触发的方法是:[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]。其他初始化和设定相关属性的方法都没有任何的变化。

3. 把设定过的notification都现实在TableView里

之前使用了一些dummy的方法,只是为了能让app正常的运行。这里补全所有必要的代码。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[UIApplication sharedApplication] scheduledLocalNotifications].count;
} - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
} NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *n = notifications[indexPath.row]; cell.textLabel.text = n.alertBody;
cell.detailTextLabel.text = [n.fireDate description]; return cell;
}

[[UIApplication sharedApplication] scheduledLocalNotifications]包含了全部的schedule过了的notification数组,count属性就是数量。在- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中从notification数组中取出对应元素,显示在UITableView的Cell中,分别是:cell.textLabel.text = n.alertBody, cell.detailTextLabel.text = [n.fireDate description]。

最后

似乎是一切都完美了。notification也有了,可以通过界面的DatePicker指定时间,可以立即触发,还可以指定提示的内容。而且,还会在“铛”的一声,然后在app的图标上显示一个badge number出来。但是,当你点击了提示之后,进入到app里再出来你会发现。。。。。。badge number还是华丽丽的在那里木有消失。是的本来应该点开了notification之后就消失的,这是为什么呢。处理badge number的代码也需要我们手动的写出来。最后我们来处理这一部分的功能。

这里需要注意到两种情况:1. 设定好了notification的出发时间以后,app退出了。这个退出是说双击home键以后,上滑了这个app,终止了app进程。2. app还在运行着,也许是在触发notification的时候app还在运行中,或者app的进程还在运行中。

第一种情况:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; BVRootViewController *rootViewController = [[BVRootViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = nav; self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible]; application.applicationIconBadgeNumber = 0; UILocalNotification *n = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (n) {
DLog(@"Received notification %@", n);
} return YES;
}

第二种情况:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
application.applicationIconBadgeNumber = 0;
DLog(@"Received notification %@", notification);
}

哦了!

Local Notification的更多相关文章

  1. ios push local notification

    UILocalNotification* localNotification = [[UILocalNotification alloc]init]; localNotification.alertB ...

  2. iOS 远程通知(Remote Notification)和本地通知(Local Notification)

    ios通知分为远程通知和本地通知,远程通知需要连接网络,本地通知是不需要的,不管用户是打开应用还是关闭应用,我们的通知都会发出,并被客户端收到 我们使用远程通知主要是随时更新最新的数据给用户,使用本地 ...

  3. cordova local notification plugin

    cordova plugin add org.apache.cordova.device cordova plugin add https://github.com/katzer/cordova-pl ...

  4. iOS 10.0之前和之后的Local Notification有神马不同

    在iOS 10.0之前apple还没有将通知功能单独拿出来自成一系.而从10.0开始原来的本地通知仍然可用,只是被标记为过时.于是乎我们可以使用10.0全新的通知功能.别急-让我们慢慢来,先从iOS ...

  5. [Xamarin] 關於發出Notification 的大小事 (转帖)

    關於Anroid 的使用者來說,Notification 是一個非常會看到且用到的功能 他可以提醒使用者甚麼東西需要待處理,像是郵件或是會議的提醒等.. 甚至有些APP ,直接使用Notificati ...

  6. ios notification

    apps can use local or push notifications to let people know when interesting things happen, such as: ...

  7. [转载]iOS 10 UserNotifications 框架解析

    活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...

  8. ios 消息通知

    苹果的通知分为本地通知和远程通知,这里主要说的是远程通知 历史介绍 iOS 3 - 引入推送通知UIApplication 的 registerForRemoteNotificationTypes 与 ...

  9. iOS 多任务

    本文转自猫神的博客:https://onevcat.com/2013/08/ios7-background-multitask/ 写的没的说,分享给大家,一起学习! iOS7以前的Multitaski ...

随机推荐

  1. 视频编解码---x264用于编码,ffmpeg用于解码

    项目要用到视频编解码,最近半个月都在搞,说实话真是走了很多弯路,浪费了很多时间.将自己的最终成果记录于此,期望会给其他人提供些许帮助. 参考教程: http://ffmpeg.org/trac/ffm ...

  2. MySql入门(1)

    环境变量的重要性环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程 ...

  3. servlet对应.net中的http上下文

    java中的servlet在.net中其实就是http上下文.

  4. module解析过程

    加载一个核心模块时 直接require('模块名')即可 加载一个文件模块时 直接require('绝对路径/相对路径')即可,可省略文件后缀.js. 因为如果文件不存在,将试图找文件名.js的文件 ...

  5. PHP mysql client封装

    config1.inc.php $CONFIG_DATABASE_TXL = array( #array('127.0.0.1', 'root', '', 'he_txl','3306') array ...

  6. SVN更新或提交时出现冲突该如何解决

    解决版本冲突的命令.在冲突解决之后,需要使用svnresolved来告诉subversion冲突解决,这样才能提交更新.冲突发生时,subversion会在WorkCopy中保存所有的目标文件版本(上 ...

  7. ubuntu 开机 输入密码 无法进入

    1.给笔记本装了ubuntu14.04.4, 发现开机到输入密码的环节之后,验证正确,然而无法进入桌面,一直在密码页循环. 2.网上找了好多方法,进入命令行(ctrl+alr+F1)登录,能登录进去: ...

  8. Spring实战之装配Bean

    1.1Spring配置的可选方案 Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系.但是,作为开发人员,你需要告诉Spring要创建哪些bean并且如何将其装配在一起.当 ...

  9. 03-22 Ajax验证用户登录

    在网页中一般是通过表单提交数据,而表单获取信息,抛弃当前页面重新加载一个新页面. 现在,在webform网页中可以通过JueryAjax提交.处理数据的方式,达到异步刷新页面. 表单提交数据和Juer ...

  10. Redis实战——Redis的pub/Sub(订阅与发布)在java中的实现

    借鉴:https://blog.csdn.net/canot/article/details/51938955 1.什么是pub/sub Pub/Sub功能(means Publish, Subscr ...