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

闲话少叙,代码如下:

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. electron 的窗口设置最大化 最小化

    /** * Created by Administrator on 2016/11/23. * 页面对窗口的一些操作封装,用于渲染进程 */ "use strict"; const ...

  2. c++友元函数之---一种特殊的友情

    类可以允许其他类或者函数访问它的私有成员,方法是令其他类或者函数成为它的友元.如果类想把一个函数或者类声明成它的友元,只需要增加一条以friend关键字开始的声明语句即可. 友元声明只能出现在类定义的 ...

  3. perl 计算方差中值平均数 Statistics::Descriptive;

    http://search.cpan.org/~shlomif/Statistics-Descriptive-3.0612/lib/Statistics/Descriptive.pm use Stat ...

  4. 基于html5鼠标悬停图片动画展示效果

    分享一款基于html5鼠标悬停图片动画展示效果.里面包含两款不同效果的html5图片展示效果.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class=" ...

  5. 几行css3代码实现超炫加载动画

    之前为大家分享了css3实现的加载动画.今天为大家带来一款只需几行代码就可以实现超炫的动画加载特效.我们一起看下效果图: 在线预览   源码下载 实现代码: 极简的html代码: <div> ...

  6. Ubuntu通过apt-get安装指定版本和查询指定软件有多少个版本

    一.通过apt-get安装指定版本 apt-get install <<package name>>=<<version>> 二.查询指定软件有多少个版 ...

  7. 基于Discuz的原生态MVC框架Uxf

    前言 国内大量网站在使用Discuz程序,当然,大多数网站只将其作为一款论坛程序,少量的将其作为一款完整的社区程序.因此,授权不授权的情况撇开不谈,很多人都会基于该款程序进行二次开发. Discuz二 ...

  8. JVM监控添加参数

    -Dcom.sun.management.jmxremote.port                           远程主机端口号的-Dcom.sun.management.jmxremote ...

  9. Akka 编程: 什么是Actor

    上一篇我们简介了Actor系统.说明了Actor之间存在着层次关系,它也是构成Actor应用的最主要的单位. 本篇介绍Actor本身的一些基本概念.一个Actor包括了State(状态),Behavi ...

  10. FastDFS - 文件服务器学习资料

    FastDFS搭建及java整合代码 CentOS 6.5下 FastDFS结合Nginx插件实现图片http访问 图片服务器fastDFS的搭建以及配置 nginx fastdfs 配置后 上传成功 ...