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都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...
随机推荐
- js和jquery如何获取图片真实的宽度和高度
按照插入的图片的尺寸来判断图片是横图还是竖图.然后判断过后给予不同的展示方式,下面为大家介绍下js和jquery如何获取图片真实的宽度和高度 1.什么时候需要获取图片真实的宽度和高度 在做pc网页 ...
- vue2.0 开发实践总结之入门篇
vue2.0 据说也出了很久了,博主终于操了一次实刀. 整体项目采用 vue + vue-router + vuex (传说中的vue 全家桶 ),构建工具使用尤大大推出的vue-cli 后续文 ...
- Caliburn.Micro学习笔记(一)----引导类和命名匹配规则
Caliburn.Micro学习笔记目录 用了几天时间看了一下开源框架Caliburn.Micro 这是他源码的地址http://caliburnmicro.codeplex.com/ 文档也写的很详 ...
- 日志文件清理工具V1.1
上次做完日志文件清理工具V1.0 的版本后,确实给自己的工作带来不少的方便.虽然只是一个小工具,代码也比较简单,但有用就是好东西.上次开发比较匆忙,有些细节没来得及完善,今天吃完晚饭,边看亚冠比赛边把 ...
- C#进阶系列——MEF实现设计上的“松耦合”(一)
前言:最近去了趟外地出差,介绍推广小组开发的框架类产品.推广对象是本部门在项目上面的同事——1到2年工作经验的初级程序员.在给他们介绍框架时发现很多框架设计层面的知识他们都没有接触过,甚至没听说过,这 ...
- Go学习
简介 Go语言是Google出了一个语言,基本概念我就不介绍了, GO语言从原生上支持高并发,并提供了简单的调用方式,我们就重点研究一下它的高并发 进程与线程 在介绍高并发之前,我们需要了解一下我们现 ...
- php魔术方法使用场景
php魔术方法-----__tostring(),__invoke,__call(),__callStatic ... __tostring(),__invoke() __tostring()方法是在 ...
- 触发器运用示例---laobai
1 触发器 概念:trigger.逻辑对象的一种.当dml的增删改语句执行时,自动触发一系列动作. 分类:dml触发器.ddl触发器(很少见) sql:ddl,dml,dcl 按触发的时间分: 语句执 ...
- Linux系统概述
1.Linux是一套免费使用 和自 由传播的类Unix操作系统. 这个系统是由世界各地的成千上万的程序员 设计和实现的.其目 的是建立不受任何商品化软件的版权制约的. 全世界都能自 由使用的Unix兼 ...
- Angular.js实现折叠按钮的经典指令.
var expanderModule=angular.module('expanderModule',[]) expanderModule.directive('expander',function( ...