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

闲话少叙,代码如下:

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. ASP.NET MVC4企业级实战目录

    http://www.cnblogs.com/jiekzou/p/5625762.html#!comments ******************************************** ...

  2. 【转】VMware虚拟机中CentOS设置固定IP

    因为需要配置固定IP,在网上找了很久终于找到一个可行的例子,自己配置成功了. 1.首先获取你的GATEWAY 方便后面在cento系统配置里使用选取菜单栏:Edit->Virtual Netwo ...

  3. shell替换掉两个以上的空格

    方法一:sed 's/ \+/ /g' test.txt > test1.txt

  4. Python实现二叉树及其4种遍历

    Python & BinaryTree 1. BinaryTree (二叉树) 二叉树是有限个元素的集合,该集合或者为空.或者有一个称为根节点(root)的元素及两个互不相交的.分别被称为左子 ...

  5. JAVA-JSP指令元素之page指令

    相关资料:<21天学通Java Web开发> 结果总结:1.page设定JSP页面全局属性,作用于整个JSP页面,包括静态包含的文件2.<%@ page 属性1="属性值1 ...

  6. iOS导航栏背景,标题和返回按钮文字颜色

    在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...

  7. MFC中Carray的使用

    CArray 需要包含的头文件 <afxtempl.h> CArray类支持与C arrays相似的数组,但是必要时可以动态压缩并扩展.数组索引从0开始.可以决定是固定数组上界还是允许当添 ...

  8. Linux下chmod命令

    命令格式 参数 描述 u User,即文件或目录的拥有者 g Group,即文件或目录的所属群组 o Other,除了文件或目录拥有者或所属群组之外,其它用户皆属于这个范围 a All,即全部的用户, ...

  9. MD5算法实现

    MD5算法的简要叙述为: MD5以512位分组来处理输入的信息(512位分组?每次处理都取出512位数据?), 每一分组又被划分为16个32位子分组(16乘32刚好是512), 经过一些列的处理后(怎 ...

  10. mysql 5.7.12----bin/mysqld --initialize --user=mysql出错

    我最近在安装mysql 5.7.12,本来之前安装mysql 5.7.11时用命令 bin/mysqld --initialize --user=mysql 可以很好的初始化,但是用在5.7.12版本 ...