ios 开发之本地推送
网络推送可能被人最为重视,但是本地推送有时候项目中也会运用到;
闲话少叙,代码如下:
1、添加根视图
self.window.rootViewController = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
2、本地创建一个button进行触发
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , WIDTH - , );
button.backgroundColor = [UIColor blueColor];
[button setTitle:@"开始啦" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//绑定方法
[button addTarget:self action:@selector(noticClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
3、注册一个通知
//设置本地通知 传一个时间进去
+ (void)registerLocalNotification:(NSInteger)alertTime
{
UILocalNotification *notification = [[UILocalNotification alloc]init];
//设置触发通知的时间
NSDate *fireDate = [NSDate dateWithTimeIntervalSinceNow:alertTime];
NSLog(@"fireDate=%@",fireDate); notification.fireDate = fireDate;
//时区
notification.timeZone = [NSTimeZone defaultTimeZone];
//设置重复的间隔
notification.repeatInterval = kCFCalendarUnitSecond; //通知内容
notification.alertBody = @"该起床了...";
notification.applicationIconBadgeNumber = ;
//通知被触发时播放的声音
notification.soundName = UILocalNotificationDefaultSoundName;
//通知参数
NSDictionary *userDict = [NSDictionary dictionaryWithObject:@"起床了,开始学习了ios开发了" forKey:@"key"];
notification.userInfo = userDict; //ios8 以后,需要添加这个注册,才能得到授权
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type
categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSCalendarUnitDay;
} else {
// 通知重复提示的单位,可以是天、周、月
notification.repeatInterval = NSDayCalendarUnit;
} //执行通知注册
[[UIApplication sharedApplication]scheduleLocalNotification:notification]; }
4、调用这个方法
-(void)noticClick:(id)sender
{
//调用通知
[ViewController registerLocalNotification:];//4秒钟后
}
5、取消通知的方法
//取消某个本地推送通知
+(void)cancelLocalNotificationWithKey:(NSString *)key
{
//获取所有本地通知数组
NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications; for (UILocalNotification *notification in localNotifications) {
NSDictionary *userInfo = notification.userInfo;
if (userInfo) { //根据设置通知参数时指定的key来获取通知参数
NSString *info = userInfo[key]; //如果找到需要取消的通知,则取消
if (info != nil) {
[[UIApplication sharedApplication]cancelLocalNotification:notification];
break;
} }
}
}
6、调用这个方法
//本地通知回调函数,当应用程序在前台时调用
-(void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification
{
NSLog(@"notif:%@",notification); //这里真是需要处理交互的地方
//获取通知所带的数据
NSString *notMes = [notification.userInfo objectForKey:@"key"];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"本地通知(前台)" message:notMes delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show]; //更新显示的角标个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= ? badge : ;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge; //在不许要再推送时,可以取消推送
[ViewController cancelLocalNotificationWithKey:@"key"];
}
ios 开发之本地推送的更多相关文章
- 李洪强iOS开发之极光推送JPush
李洪强iOS开发之极光推送JPush
- iOS开发之远程推送
说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...
- iOS开发:创建推送开发证书和生产证书,以及往极光推送官网上传证书的步骤方法
在极光官网上面上传应用的极光推送证书的实质其实就是上传导出的p12文件,在极光推送应用管理里面,需要上传两个p12文件,一个是生产证书,一个是开发证书 ,缺一不可,具体如下所示: 在开发者账号里面创建 ...
- iOS开发——远程消息推送的实现
在我们使用App的过程中.总是会收到非常多的消息推送.今天我们就要来实现这个功能.首先消息推送分为本地消息推送和远程消息推送.而当中又以远程消息最为经常使用. 可是在推送远程消息之前.有两个前提条件. ...
- ios 开发之 -- 极光推送,发送自定义消息,进入制定页面
在进行极光推送时候,发现版本有所更新,以前截取didfinish入口方法里面的launchOptions,获取一个本地的通知内容,进行本地展示不可用了,通过查询官方文档和网上的资料才发现,方法改变了, ...
- iOS开发资源:推送通知相关开源项目--PushSharp、APNS-PHP以及Pyapns等
PushSharp (github) PushSharp是一个实现了由服务器端向移动客户端推送消息的开源C#库,支持 iOS (iPhone/iPad APNS). Android (C2DM/GC ...
- iOS开发——百度云推送
由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...
- iOS开发本地推送(iOS10)UNUserNotificationCenter
1.简介 iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了.简单介绍本地推送的使用UserNotifications官方文档说明! 2.简单使用UNUs ...
- iOS开发本地推送
1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...
随机推荐
- ubuntu文件夹默认列表显示
编辑-->首选项-->视图-->列表视图
- highcharts 动态生成x轴和折线图
highchart 动态生成x轴和折线图 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8&qu ...
- JAVA经典算法40题面向过程
JAVA经典算法40题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分 ...
- java质量提升相关
http://blog.sina.com.cn/s/blog_6814a1510102v2rp.html
- JS地毯式学习一
1.<noscript> 现代浏览器都对JavaScript进行了支持,一般是在用户的浏览器禁用了脚本的情况下才会显示<noscript>的内容. 包含在<noscrip ...
- 【C#/WPF】Button按钮动态设置Background背景颜色
学习笔记: 在XAML中给Button设置颜色大家都懂的,本篇只是记录用C#代码动态生成的按钮设置Background背景颜色. new一个Button,设置Background时可看到该属性类型是S ...
- C语言 · 求圆面积表面积体积
算法提高 3-3求圆面积表面积体积 时间限制:1.0s 内存限制:256.0MB 问题描述 接受用户输⼊的数值,输出以该值为半径的(1)圆面积,(2)球体表面积,(3)球体体积.pi ...
- SpringMVC @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 http://blog.csdn.net/walkerjong/article/details/7994326
- C++实现 逆波兰表达式计算问题
C++实现 逆波兰表达式计算问题 #include <iostream> #include <string> using namespace std; class Stack ...
- apt-get install 的替换命令及mysql安装问题的解决
Some packages could not be installed. This may mean that you haverequested an impossible situation o ...