IOS 本地推送(UILocalNotification)
推送通知
• 推送通知是可见的(能用肉眼看到)
● iOS中提供了2种推送通知
● 本地推送通知(Local Notification)
● 远程推送通知(Remote Notification)
● 推送通知的作用
● 在屏幕顶部显示一块横幅(显示具体内容)
● 在屏幕中间弹出一个UIAlertView(显示具体内容)
● 在锁屏界面显示一块横幅(锁屏状态下,显示具体内容)
● 播放音效(提醒作用)
● 发出推送通知时,如果程序正运行在前台,那么推送通知就不会被呈现出来
● 不管app打开还是关闭,推送通知都能如期发出
● 顾名思义,就是不需要联网就能发出的推送通知(不需要服务器的支持)
● 本地推送通知的使用场景
● 常用来定时提醒用户完成一些任务,比如
• 清理垃圾、记账、买衣服、看电影、玩游戏
如何发出本地推送通知
UILocalNotification *ln = [[UILocalNotification alloc] init];
● 设置本地推送通知属性
● 推送通知的触发时间(何时发出推送通知)
@property(nonatomic,copy) NSDate *fireDate;
● 推送通知的具体内容
@property(nonatomic,copy) NSString *alertBody;
● 锁屏界面显示的小标题(完整小标题:“滑动来” + alertAction)
@property(nonatomic,copy) NSString *alertAction;
● 音效文件名
@property(nonatomic,copy) NSString *soundName;
● app图标数字
@property(nonatomic) NSInteger applicationIconBadgeNumber;
● 获得被调度的所有本地推送通知(等待发出的通知)
@property(nonatomic,copy) NSArray *scheduledLocalNotifications;
(已经发出且过期的推送通知就算调度结束,会自动从这个数组中移除)
● 取消调度本地推送通知
- (void)cancelLocalNotification:(UILocalNotification *)notification;
- (void)cancelAllLocalNotifications;
● 立即发出本地推送通知(使用价值:app在后台运行的时候)
- (void)presentLocalNotificationNow:(UILocalNotification *)notification;
本地推送通知的其他属性
@property(nonatomic) NSCalendarUnit repeatInterval;
● 点击推送通知打开app时显示的启动图片
@property(nonatomic,copy) NSString *alertLaunchImage;
● 附加的额外信息
@property(nonatomic,copy) NSDictionary *userInfo;
● 时区
@property(nonatomic,copy) NSTimeZone *timeZone;
(一般设置为[NSTimeZone defaultTimeZone] ,跟随手机的时区)
点击本地推送通知
● app并没有关闭,一直隐藏在后台
• 让app进入前台,并会调用AppDelegate的下面方法(并非重新启动app)
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification;
● app已经被关闭(进程已死)
• 启动app,启动完毕会调用AppDelegate的下面方法
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
● launchOptions参数通过UIApplicationLaunchOptionsLocalNotificationKey 取出本地推送通知对象
实例:
#import "HMAppDelegate.h"
#import "HMViewController.h" @interface HMAppDelegate()
@property (nonatomic, weak) UILabel *label;
@end @implementation HMAppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// NSLog(@"------didFinishLaunchingWithOptions---%@"); UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor redColor];
label.frame = CGRectMake(, , , );
label.font = [UIFont systemFontOfSize:];
label.numberOfLines = ;
[[[self.window.rootViewController.childViewControllers firstObject] view] addSubview:label]; UILocalNotification *note = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (note) {
label.text = @"点击本地通知启动的程序"; HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
[homeVc performSegueWithIdentifier:@"home2detail" sender:note];
} else {
label.text = @"直接点击app图标启动的程序";
}
self.label = label;
return YES;
} /**
* 当用户点击本地通知进入app的时候调用 、通知发出的时候(app当时并没有被关闭)
*/
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// self.label.text = [NSString stringWithFormat:@"点击通知再次回到前台---%d", application.applicationState];
// 程序正处在前台运行,直接返回
if (application.applicationState == UIApplicationStateActive) return; HMViewController *homeVc = [self.window.rootViewController.childViewControllers firstObject];
[homeVc performSegueWithIdentifier:@"home2detail" sender:notification];
} @end
#import "HMViewController.h"
#import "HMDetailViewController.h" @interface HMViewController ()
- (IBAction)schedule;
- (IBAction)cancel;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
} - (IBAction)schedule2 {
// 1.创建本地推送通知对象
UILocalNotification *ln = [[UILocalNotification alloc] init]; // 2.设置通知属性 // 音效文件名
ln.soundName = @"buyao.wav"; // 通知的具体内容
ln.alertBody = @"重大新闻:xxxx xxxx被调查了...."; // 锁屏界面显示的小标题("滑动来" + alertAction)
ln.alertAction = @"查看新闻吧"; // 设置app图标数字
ln.applicationIconBadgeNumber = ; [[UIApplication sharedApplication] presentLocalNotificationNow:ln];
} - (IBAction)schedule {
// 1.创建本地推送通知对象
UILocalNotification *ln = [[UILocalNotification alloc] init]; // 2.设置通知属性
// 音效文件名
ln.soundName = @"buyao.wav"; // 通知的具体内容
ln.alertBody = @"重大新闻:xxxx xxxx被调查了...."; // 锁屏界面显示的小标题("滑动来" + alertAction)
ln.alertAction = @"查看新闻吧"; // 通知第一次发出的时间(5秒后发出)
ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:];
// 设置时区(跟随手机的时区)
ln.timeZone = [NSTimeZone defaultTimeZone]; // 设置app图标数字
ln.applicationIconBadgeNumber = ; // 设置通知的额外信息
ln.userInfo = @{
@"icon" : @"test.png",
@"title" : @"重大新闻",
@"time" : @"2014-08-14 11:19",
@"body" : @"重大新闻:答复后即可更换就肯定会尽快赶快回家的疯狂估计很快将发的"
}; // 设置启动图片
ln.alertLaunchImage = @"Default"; // 设置重复发出通知的时间间隔
// ln.repeatInterval = NSCalendarUnitMinute; // 3.调度通知(启动任务)
[[UIApplication sharedApplication] scheduleLocalNotification:ln];
} - (IBAction)cancel {
NSArray *notes = [UIApplication sharedApplication].scheduledLocalNotifications;
NSLog(@"%@", notes);
// [[UIApplication sharedApplication] cancelAllLocalNotifications];
} - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(UILocalNotification *)note
{
HMDetailViewController *detailVc = segue.destinationViewController;
detailVc.userInfo = note.userInfo;
}
@end
IOS 本地推送(UILocalNotification)的更多相关文章
- [转载]iOS本地推送-备用
第一步:创建本地推送// 创建一个本地推送UILocalNotification *notification = [[[UILocalNotification alloc] init] autorel ...
- 本地推送UILocalNotification(转)
1.增加一个本地推送 //设置20秒之后 NSDate *date = [NSDate dateWithTimeIntervalSinceNow:]; //chuagjian一个本地推送 UILoca ...
- (七十三)iOS本地推送通知的实现
iOS的推送通知分为本地推送和网络推送两种,如果App处于挂起状态,是可以发送本地通知的,如果已经被杀掉,则只有定时通知可以被执行,而类似于QQ的那种网络消息推送就无法实现了,因为App的网络模块在被 ...
- 81、iOS本地推送与远程推送详解
一.简介 分为本地推送和远程推送2种.可以在应用没打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户石否同意,如果同意则正常使用:如果用户不同意则下次打开程序 ...
- iOS 本地推送通知
1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...
- 本地推送UILocalNotification
//本地推送---无需网络,由本地发起 UILocalNotification *localNotification = [[UILocalNotification alloc]init]; //设置 ...
- iOS本地推送与远程推送
原文在此 分为本地推送和远程推送2种.可以在应用没有打开甚至手机锁屏情况下给用户以提示.它们都需要注册,注册后系统会弹出提示框(如下图)提示用户是否同意,如果同意则正常使用:如果用户不同意则下次打开程 ...
- Swift - 推送之本地推送(UILocalNotification)添加Button的点击事件
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息 import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- Swift - 推送之本地推送(UILocalNotification)
// 本地推送通知是通过实例化UILocalNotification实现的.要实现本地化推送可以在AppDelegate.swift中添加代码实现,本事例是一个当App进入后台时推送一条消息给用户. ...
随机推荐
- 【实战】SSL和TLS漏洞验证
工具下载:git clone https://github.com/drwetter/testssl.sh.git 实验环境:192.168.1.22(bee-box v1.6) 192.168.1. ...
- 3.Exadata 软件体系结构
整体架构和 smart scan Aasm Ehcc (混合例压缩 和 存储索引) SCAN Service 和 server pool DB SERVER -> DB instance -&g ...
- vue之mapMutaions的使用 && vuex中 action 用法示例 && api.js的使用
vue之mapMutations的使用 我们通过Mutation来改变store中的state,方法往往是在子组件中使用 this.$store.commit(); 来实现,但是这样的缺点是不容易查看 ...
- LeetCode 257.二叉树所有路径(C++)
给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / \ 2 3 \ 5 输出: ["1->2->5", ...
- HikariCP配置使用spring结合--Java数据库连接池
我的个人德州扑克项目https://github.com/mingzijian/pokers,欢迎给星星.maven引入: Java 8 maven artifact: <dependency& ...
- Android学习06Android应用程序的基本组件
一个Android应用程序可以由几个不同的组件构成,Android应用程序的基本组件包括:Activity,Service,BroadcastReceiver,ContentProvider和Inte ...
- 东拼西凑 vim配置-更新
"============================================================= "========================== ...
- JS字符串与二进制的相互转化
//字符串转ascii码,用charCodeAt(); //ascii码转字符串,用fromCharCode(); var str = "A"; var code = str.ch ...
- Android开发_如何调用系统默认浏览器访问
Android开发_如何调用系统默认浏览器访问 2015-10-20 17:53 312人阅读 http://blog.sina.com.cn/s/blog_6efce07e010142w7.htm ...
- What's the difference between @Component, @Repository & @Service annotations in Spring?
@Component is equivalent to <bean> @Service, @Controller , @Repository = {@Component + some mo ...