本文所写方法主要应用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本地通知的使用方法的更多相关文章

  1. iOS开发中UILocalNotification本地通知实现简单的提醒功能

    这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app ...

  2. UILocalNotification本地通知

    // 执行通知一定要退出应用或挂起应用(进入后台)才能收到通知. 1.在iOS8及其以后版本中使用本地消息需要先获得用户的许可,否则无法成功注册本地消息.因此,我们将询问用户许可的代码片段添加到了ap ...

  3. Ios开发中UILocalNotification实现本地通知实现提醒功能

    这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time ...

  4. ios推送:本地通知UILocalNotification

    Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notificati ...

  5. IOS本地通知:UILocalNotification使用记录

    第一次接触IOS的本地通知的使用,看到别人写的一个比较详细的记录,自己整理过来,方便以后再次使用和拓展: 1.创建一个本地通知,添加到系统: // 初始化本地通知对象 UILocalNotificat ...

  6. IOS 本地通知 UILocalNotification

    IOS 本地通知 UILocalNotification [本文章第四部分中的代码逻辑来自网上的借鉴,并非我自己原创] 大概一个月前,我开始跟着做IOS项目了.学习C++,了解Objective-C, ...

  7. 本地通知UILocalNotification

    1.增加一个本地推送 //设置20秒之后  ]; //chuagjian一个本地推送 UILocalNotification *noti = [[[UILocalNotification alloc] ...

  8. 本地通知-UILocalNotification

    第一步:创建本地推送 本地通知 UILocalNotification // 创建⼀一个本地推送 UILocalNotification * notification = [[UILocalNotif ...

  9. IOS 本地通知推送消息

    在现在的移动设备中,好多应用性的APP都用到了推送服务,但是有好多推送的内容,比如有的只是单纯的进行推送一个闹钟类型的,起了提醒作 用,有的则是推送的实质性的内容,这就分为推送的内容来区别用什么推送, ...

随机推荐

  1. TEXTAREA自适应文字行数的多少

    <textarea rows=1 name=s1 cols=27 onpropertychange="this.style.posHeight=this.scrollHeight&qu ...

  2. 分享一个自制的 .net线程池

    扯淡 由于项目需求,需要开发一些程序去爬取一些网站的信息,算是小爬虫程序吧.爬网页这东西是要经过网络传输,如果程序运行起来串行执行请求爬取,会很慢,我想没人会这样做.为了提高爬取效率,必须使用多线程并 ...

  3. iOS小知识点(UI部分)

    1. 父视图通过Tag来找到UIView UIView *targetView = [superView viewWithTag:10];//只在当前视图以及subviews中找,不能再孙子中找. 2 ...

  4. vue.js之绑定class和style

    一.绑定Class属性. 绑定数据用v-bind:命令,简写成: 语法:<div v-bind:class="{ active: isActive }"></di ...

  5. BZOJ 1432: [ZJOI2009]Function

    1432: [ZJOI2009]Function Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1046  Solved: 765[Submit][Sta ...

  6. 【codevs2495】 水叮当的舞步

    http://codevs.cn/problem/2495/ (题目链接) 题意 给出一个N*N的矩阵,其中元素有5种颜色,每次可以将左上角元素所在的连通块更换一种颜色,连通块指相邻并且颜色相同的元素 ...

  7. arcgis engine 监听element的添加、更新和删除事件(使用IMovePointFeedback)

    VB代码: 复制进程序稍作修改变量名和事件逻辑即可使用. Members   AllPropertiesMethodsInheritedNon-inherited Description Displa ...

  8. 第二轮冲刺-Runner站立会议08

    今天:优化日历界面 明天:将日历界面与主程序结合

  9. Windows远程数据同步工具cwRsync

    1. cwRsync简介cwRsync是Rsync在Windows上的实现版本,Rsync通过使用特定算法的文件传输技术,可以在网络上传输只修改了的文件.cwRsync主要用于Windows上的远程文 ...

  10. 生成彩条的MATLAB代码

    clc;close all;clear %read image % RGBimga = imread('bmpinput_1080p.bmp'); RGBimga = imread('bmpinput ...