UILocalNotification本地通知的使用方法
本文所写方法主要应用UILocalNotification达到本地推送通知栏信息
取消了其他教程里过期的UIAlertView方法
使用UILocalNotification主要分为创建 调用 取消 三个步骤
同时注意 如果调用[NSDate dateWithTimeIntervalSince1970:alertTime]这个方法 这个时间不是从显示1970年1月1日开始计算 而是1970年1月1日8点开始计算
具体详见格林威治时间相关信息
1.创建UILocalNotification 分别在AppDelegate和具体实现通知的Controller中写入以下代码 需要注意的是创建方法中的Key值 是用于后面取消时候的标记
并且同时要注意 每次添加新的通知 要把以前的通知去掉 否则以前的通知不会被新的通知覆盖 即两个通知会同时存在
AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//取消徽章
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:];
}
#pragma mark 本地通知回调函数 当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//更新显示的徽章个数
NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
badge--;
badge = badge >= ? badge : ;
[UIApplication sharedApplication].applicationIconBadgeNumber = badge;
//取消通知推送
[MainViewController cancelLocalNotificationWithKey:@"weather"];
}
Controller #pragma mark 本地通知功能
+ (void)registerLocalNotification:(NSInteger)alertTime {
//建立本地通知对象
UILocalNotification *notification=[[UILocalNotification alloc]init];
//设置触发通知的时间
NSDate *fireDate=[NSDate dateWithTimeIntervalSince1970:alertTime];
NSLog(@"触发通知的时间=%@",fireDate);
notification.fireDate=fireDate;
//设置时区
notification.timeZone=[NSTimeZone defaultTimeZone];
//设置重复的间隔
notification.repeatInterval=kCFCalendarUnitDay;
//设置通知内容
notification.alertBody=@"早安哦~今天也很想你";
notification.applicationIconBadgeNumber=;
//通知被触发时播放的声音
notification.soundName=UILocalNotificationDefaultSoundName;
//创建本地通知的info信息 用于取消通知
NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"weather"];
notification.userInfo = info;
//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];
} #pragma mark 取消某个本地通知
+ (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;
}
}
}
}
2.调用UILocalNotification 因为上面代码把调用方法封装成了类方法 直接用相应的Controller类直接调用
#pragma mark 调用本地通知方法
- (void)localNotification
{
//调用本地通知方法
[MainViewController registerLocalNotification:p];
NSLog(@"开启本地通知");
}
3.取消UILocalNotification 取消方法同理 也是类方法的调用 根据定义时的方法中Key值取消相应的通知
-(void)notificationSwitch
{
if (noticeSwitch.on==YES) {
//调用本地通知
[self localNotification];
NSLog(@"开启本地通知");
}
if (noticeSwitch.on==NO) {
[MainViewController cancelLocalNotificationWithKey:@"weather"];
NSLog(@"关闭本地通知");
}
}
UILocalNotification本地通知的使用方法的更多相关文章
- iOS开发中UILocalNotification本地通知实现简单的提醒功能
这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...
- UILocalNotification本地通知
// 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...
- Ios开发中UILocalNotification实现本地通知实现提醒功能
这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...
- ios推送:本地通知UILocalNotification
Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notificati ...
- IOS本地通知:UILocalNotification使用记录
第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...
- IOS 本地通知 UILocalNotification
IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...
- 本地通知UILocalNotification
1.增加一个本地推送 //设置20秒之后 ]; //chuagjian一个本地推送 UILocalNotification *noti = [[[UILocalNotification alloc] ...
- 本地通知-UILocalNotification
第一步:创建本地推送 本地通知 UILocalNotification // 创建⼀一个本地推送 UILocalNotification * notification = [[UILocalNotif ...
- IOS 本地通知推送消息
在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
随机推荐
- Visual Studio 2015的坑:中文字符串编译后成乱码
(2015年8月5日更新:微软已经修复了Roslyn的这个bug,详见 https://github.com/dotnet/roslyn/pull/4303 ) 昨天,我们用VS2015编译了博客程序 ...
- 【原】关于使用sklearn进行数据预处理 —— 归一化/标准化/正则化
一.标准化(Z-Score),或者去除均值和方差缩放 公式为:(X-mean)/std 计算时对每个属性/每列分别进行. 将数据按期属性(按列进行)减去其均值,并处以其方差.得到的结果是,对于每个属 ...
- 51nod DP 最大子段和
#include<iostream> #include<algorithm> #include<cstdio> #define MAXN 50000 using n ...
- css 补漏
1.box-sizing: width(宽) + padding(内边距) + border(边框) = 元素实际宽度 height(高) + padding(内边距) + border(边框) ...
- [转]Ext ComboBox 默认选中某一项
原文地址:http://blog.csdn.net/liuguxing/article/details/8623190 项目中经常用到选择框,需要从后台异步加载数据,可单独写一个组件进行加载 App. ...
- UIActivityViewController 系统社交化 共享
1.UIActivityViewController是继承自UIViewController,是拥有VC的特性 a.初始化 init , initWithActivityItems:applicat ...
- 微信学习总结 09 解析接口中的消息创建时间CreateTime
1 消息的创建时间 网页超链接的作用以及如何在文本消息中使用网页超链接 2. 具体实现 刘峰博主的博文已经分析的很清楚了,直接去看就行了 .http://blog.csdn.net/lyq8479/a ...
- OPenCL
OpenCLhttp://baike.baidu.com/link?url=7uHWCVUYB3Sau_xh3OOKP-A08_IvmT1SJixdAXKezCuCfkzeSQDiSmesGyVGk8 ...
- Mac 效率工具
我的Mac开发环境 http://blog.csdn.net/feelang/article/details/45071249 iterm2 http://iterm2.com/documentati ...
- Spring系列之bean的使用
一.Bean的定义 <bean id="userDao" class="com.dev.spring.simple.MemoryUserDao"/> ...