网络推送可能被人最为重视,但是本地推送有时候项目中也会运用到;

闲话少叙,代码如下:

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 开发之本地推送的更多相关文章

  1. 李洪强iOS开发之极光推送JPush

    李洪强iOS开发之极光推送JPush

  2. iOS开发之远程推送

    说到远程推送,应该用的也挺多的,今天就基于SEA的云推送服务,做一个推送的小demo,来了解一下iOS中的远程推送是怎么一回事儿,首先你得有苹果的开发者账号,好咸蛋也差不多了,主要内容走起. 一.准备 ...

  3. iOS开发:创建推送开发证书和生产证书,以及往极光推送官网上传证书的步骤方法

    在极光官网上面上传应用的极光推送证书的实质其实就是上传导出的p12文件,在极光推送应用管理里面,需要上传两个p12文件,一个是生产证书,一个是开发证书 ,缺一不可,具体如下所示: 在开发者账号里面创建 ...

  4. iOS开发——远程消息推送的实现

    在我们使用App的过程中.总是会收到非常多的消息推送.今天我们就要来实现这个功能.首先消息推送分为本地消息推送和远程消息推送.而当中又以远程消息最为经常使用. 可是在推送远程消息之前.有两个前提条件. ...

  5. ios 开发之 -- 极光推送,发送自定义消息,进入制定页面

    在进行极光推送时候,发现版本有所更新,以前截取didfinish入口方法里面的launchOptions,获取一个本地的通知内容,进行本地展示不可用了,通过查询官方文档和网上的资料才发现,方法改变了, ...

  6. iOS开发资源:推送通知相关开源项目--PushSharp、APNS-PHP以及Pyapns等

    PushSharp  (github) PushSharp是一个实现了由服务器端向移动客户端推送消息的开源C#库,支持 iOS (iPhone/iPad APNS). Android (C2DM/GC ...

  7. iOS开发——百度云推送

    由于公司项目是集成的极光推送,详见下一篇博客. 集成百度推送大体相当,最好都参考官方文档集成,官方文档或官方网站教程是最好的博客. 百度Push服务SDK用户手册(iOS版) http://push. ...

  8. iOS开发本地推送(iOS10)UNUserNotificationCenter

    1.简介 iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了.简单介绍本地推送的使用UserNotifications官方文档说明! 2.简单使用UNUs ...

  9. iOS开发本地推送

    1.简介 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知. 2.创建UILocalNot ...

随机推荐

  1. 关于Solr6.0中solrj使用简单例子

    solr6.0的solrJ接口有部分变化,下面列出了简单的使用实例,有需要的朋友可以参考下. package com.ailk.solr6; import java.io.IOException; i ...

  2. Zookeeper客户端使用

    参考链接: http://blog.csdn.net/jason5186/article/details/46314381 http://ifeve.com/zookeeper-path-cache/

  3. Spark Streaming中的操作函数讲解

    Spark Streaming中的操作函数讲解 根据根据Spark官方文档中的描述,在Spark Streaming应用中,一个DStream对象可以调用多种操作,主要分为以下几类 Transform ...

  4. /dev/sdxx is apparently in use by the system; will not make a filesystem here! 解决方法

    在存储上共享了一个500G的空间,映射到Linux系统提供上,环境由2个节点组成. 一. 测试一: 直接mount 用fdisk 格式化之后如下: [root@rac1 u01]# fdisk -l ...

  5. [Django学习]视图

    视图 视图接受Web请求并且返回Web响应 视图就是一个python函数,被定义在views.py中 响应可以是一张网页的HTML内容,一个重定向,一个404错误等等 响应处理过程如下图: 1. UR ...

  6. Java中上传文件和表单数据提交如何保持数据的一致性?

    学生申请学科竞赛活动,表单中有学科竞赛的申报信息和部分附件,需要做到将上传文件和表单数据提交保持一致性. 将上传文件和插入表单数据放到事务汇总去处理,由于表单的数据我们可以控制,但是上传的文档不好控制 ...

  7. js学习笔记17----元素的各种位置,尺寸

    1. offsetLeft[Top] 当前元素到定位父级(即offsetParent)的距离(偏移值 ). 父级没有定位: offsetParent -> body offsetLeft[Top ...

  8. Windows通用知识讲解一

    Window应用程序的类型 --控制台程序Console DOS程序,本身没有窗口,通过Windows DOS窗口执行 --窗口程序 拥有自己的窗口,可以与用户交互 --库程序 存放代码.数据的程序, ...

  9. Geometric deep learning on graphs and manifolds using mixture model CNNs

    Monti, Federico, et al. "Geometric deep learning on graphs and manifolds using mixture model CN ...

  10. opencv3.2 dnn 图像分割

    下载 http://dl.caffe.berkeleyvision.org/fcn32s-heavy-pascal.caffemodel 在opencv_contrib-3.2.0/modules/d ...