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都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
随机推荐
- 微信小程序开发工具使用与设计规范(二)
[未经作者本人同意,请勿以任何形式转载] 上一篇文章主要分析了微信小程序应用场景和优劣势.本篇你可以学习到: 如何使用小程序开发工具写一个Hello World 微信小程序设计规范 微信小程序项目结构 ...
- Xamarin Mono For Android 4.6.07004 完整离线安装破解版(C#开发Android、IOS工具)
Xamarin是由Miguel de Icaza成立的一家新的独立公司,目的是给Mono一个继续奋斗的机会.Mono for Android (原名:MonoDroid)可以让开发人员使用 Mic ...
- Oracle 11g导出空表、少表的解决办法
ORACLE 11G中有个新特性,当表无数据时,不分配segment,以节省空间. 解决方法: 1)insert一行,再rollback就产生segment了 该方法是在在空表中插入数据,再删除,则产 ...
- SVN随记
SVN中提交代码时报如下错误 commit -m "sync" E:/resource/rad_workspace/IMSCrawl/src/configuration.prope ...
- Thinking in java学习笔记之final
- python函数 与 函数式编程
「函数」一词来源于数学,但编程中的「函数」概念,与数学中的函数是有很大不同的,具体区别,我们后面会讲,编程中的函数在英文中也有很多不同的叫法.在BASIC中叫做subroutine(子过程或子程序), ...
- android 手机去哪儿7.2版本客户端 账号存储信息分析
1.data/data/com.qunar sharepref 文件夹下的Qunarperferences.xml文件中 username,phone等均为加密处理过字段 2.jdgui下查找关键 ...
- Linux下因为系统编码问题造成乱码的解决办法
2016年12月13日18:34:32 -------------------------------- 最近一段时间遇到一些润乾报表的应用在linux系统下面乱码的问题,最后检查后都发现是客户的li ...
- 【整理】认识MSG结构体
在Windows程序中,消息是由MSG结构体来表示的.MSG结构体的定义如下(参见MSDN): typedef struct tagMSG { HWND hwnd; UINT message; WPA ...
- 框架集(Framesets)
1.Frameset的使用 所谓框架便是网页画面分成几个框窗,同时取得多个 URL.只 要 <FRAMESET> <FRAME> 即可,而所有框架标记 要放在一个总起的 htm ...