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 ...
随机推荐
- Zookeeper已经分布式环境中的假死脑裂
Zookeeper简介 在上班之前都不知道有这样一个东西,在开始说假死脑裂之前先说说Zookeeper吧. Zookeeper zookeeper是一个分布式应用程序的协调服务.它是一个为分布式应用提 ...
- GNU风格 ARM汇编语法3
. GNU汇编程序中的分段 <1>.section伪操作 .section <section_name> {,”<flags>”} Starts a new cod ...
- 【转】Oozie4.2.0配置安装实战
什么是Oozie? Oozie是一种Java Web应用程序,它运行在Java servlet容器——即Tomcat——中,并使用数据库来存储以下内容: 工作流定义 当前运行的工作流实例,包括实例的状 ...
- 发布了listener报404
我刚才发布了这么一个listener package org.lxh.listenerdemo ; import javax.servlet.http.* ; public class HttpSes ...
- IR的评价指标之MRR
MRR(Mean Reciprocal Rank): 是一个国际上通用的对搜索算法进行评价的机制,即第一个结果匹配,分数为1,第二个匹配分数为0.5,第n个匹配分数为1/n,如果没有匹配的句子分数为0 ...
- 【C++程序员学 python】python 之变量
既然学过C++,那么就应该知道变量是什么,常量是什么. python 相比于C++,在使用变量之前不用先声明. 而是直接使用,python 会根据你的变量自动识别其类型. 假如a = 123 那么a ...
- iOS导航栏背景,标题和返回按钮文字颜色
在iOS7下,默认导航栏背景,颜色是这样的,接下来我们就进行自定义,如果你仅仅是更改一下背景和颜色,代码会很简单,不需要很复杂的自定义View来替代leftBarItem 更改导航栏的背景和文字Col ...
- enter快捷键盘
protected override bool ProcessDialogKey(Keys keyData) { #region PageDown if (keyData == Keys.Enter) ...
- PHP MysqlI操作数据库
1连接数据库. //procedural style $mysqli = mysqli_connect('host','username','password','database_name'); / ...
- win7共享文件夹设置无密码
首先我们要启用guest账户,右键计算机 2 选择管理 3 选择本地用户和组 4 然后选择用户 5 然后选择Guest右键——属性——把账户已禁用勾掉,就可以了 6 然后点击桌面网络右键——属性 7 ...