UILocalNotification的使用——本地通知

   

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

本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

本地Notification所使用的对象是UILocalNotification,UILocalNotification的属性涵盖了所有处理Notification需要的内容。

UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、soundName和userInfo。

UILocalNotification的调度

其中fireDate、timeZone、repeatInterval和repeatCalendar是用于UILocalNotification的调度。

fireDate                 是UILocalNotification的激发的确切时间。

timeZone               是UILocalNotification
发时间是否根据时区改变而改变,如果设置为nil的话,那么UILocalNotification将在一段时候后被激发,而不是某一个确切时间被激发。

repeatInterval        是UILocalNotification被重复激发之间的时间差,不过时间差是完全根据日历单位
(NSCalendarUnit)的,例如每周激发的单位,NSWeekCalendarUnit,如果不设置的话,将不会重复激发。

repeatCalendar     是UILocalNotification重复激发所使用的日历单位需要参考的日历,如果不设置的话,系统默认的日历将被作
为参考日历。

UILocalNotification的提醒内容

alertBody、alertAction、hasAction和alertLaunchImage是当应用不在运行时,系统处理

UILocalNotification提醒是需要的内容。

alertBody              是一串现
实提醒内容的字符串(NSString),如果alertBody未设置的话,Notification被激发时将不现实提醒。

alertAction也        
是一串字符(NSString),alertAction的内容将作为提醒中动作按钮上的文字,如果未设置的话,提醒信息中的动作按钮将显示为
“View”相对文字形式。

alertLaunchImage  是在用户点击提醒框中动作按钮(“View”)时,等待应用加载时显示的图片,这个将替代应
用原本设置的加载图片。

hasAction               是一个控制是否在提醒框中显示动作按钮的布尔值,默认值为YES。

UILocalNotification的其他部分

applicationIconBadgeNumber、soundName和userInfo将使UILocalNotification更完整。

applicationIconBadgeNumber     
是显示在应用图标右上角的数字,这样让用户直接了解到应用需要处理的Notification。

soundName                                  是另一个
UILocalNotification用来提醒用户的手段,在Notification被激发之后将播放这段声音来提醒用户有Notification
需要处理,如果不设soundName的话,Notification被激发是将不会有声音播放,除去应用特制的声音以外,也可以将soundName设
为UILocalNotificationDefaultSoundName来使用系统默认提醒声音。

userInfo                                        是Notification用来传递数据的NSDictionary。

登记UILocalNotification

在设置完UILocalNotification对象之后,应用需要在系统Notification处理队列中登记已设置完的UILocalNotification对象。

登记UILocalNotification
* localNotification的方式为:

   [[UIApplication
sharedApplication]
 scheduleLocalNotification:localNotification];

在有些时候,应用可能需要直接激发一个Notification而不是等一段时间在激发,应用可以以下的方式直接触发已设好的Notification:

   [[UIApplication
sharedApplication]
presentLocalNotificationNow:localNotification];

处理UILocalNotification

在提醒框动作按钮被点击后,应用开始运行时,可以在-(BOOL)application:didFinishLaunchingWithOptions:这个Application
delegate方法中处理。

可以通过以下方式来加载为最近未处理的Notification:

   UILocalNotification *
localNotif=[launchOptions
objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

如果应用正在运行时,可以通过覆盖在Application
Delegate中的方法-(void)application:didReceiveLocalNotification:来处理Notification。作为方法的第二个参数为UILocalNotification对象,只需处理对象携带的userInfo来处理响应的动作。

取消UILocalNotification

可以使用以下两个方式来取消一个已经登记的Notification,第一个方式可以直接取消一个指定的Notification,第二个方式将会把该应用已登记的Notification一起取消

   [[UIApplication
sharedApplication]
cancelLocalNotification:localNotification];

   [[UIApplication
sharedApplication] cancelAllLocalNotification];

总结

本地Notification的机制在应用开发中非常有效,可以很好的帮助开发者管理一些指定时间需要发生的事件,例如闹钟类的应用。而且因为系统统一对Notification的管理,让同样的任务可以非常简单得被处理,而无需让应用浪费资源去等待事件的触发。

#pragma mark -

#pragma mark Local Notifications

- (void)scheduleAlarmForDate:(NSDate *)theDate {

UIApplication *app = [UIApplication sharedApplication];

NSArray *oldNotifications = [app scheduledLocalNotifications];

// Clear out the old notification before scheduling a new one.

if (0 < [oldNotifications count]) {

[app cancelAllLocalNotifications];

}

// Create a new notification

UILocalNotification *alarm = [[UILocalNotification alloc] init];

if (alarm) {

alarm.fireDate = theDate;

alarm.timeZone = [NSTimeZone defaultTimeZone];

alarm.repeatInterval = kCFCalendarUnitSecond;

alarm.soundName = @"ping.caf";//@"default";

alarm.alertBody = [NSString stringWithFormat:@"Time to wake up!Now is\n[%@]", 

  [NSDate dateWithTimeIntervalSinceNow:10]];

alarm.hasAction = NO;

//        alarm.alertAction = @"jjjjjj";

        [alarm setAlertLaunchImage:@"taopiao.png"];

[app scheduleLocalNotification:alarm];

[alarm release];

}

} 

示例参考:http://www.cnblogs.com/jiangshiyong/archive/2012/06/06/2538204.html

时间: 2024-12-20 09:19:31

UILocalNotification的使用——本地通知的相关文章

UILocalNotification本地通知

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

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

这两天在做一个日程提醒功能,用到了本地通知的功能,记录相关知识如下: 1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间: notification type,通知类型,包括警告信息.动作按钮的标题.应用图标上的badge(数字标记)和播放的声音: 自定义数据,本地通知可以包含一个dictionary类型的本地数据. 对本地通知的数量限制,iOS最多允许最近本地通知数

IOS 本地通知UILocalNotification

//发送通知    UILocalNotification *notification=[[UILocalNotification alloc] init];       if (notification!=nil) {         NSDate *now=[NSDate new];         notification.fireDate=[now dateByAddingTimeInterval:10];//10秒后通知        notification.repeatInterv

ios推送:本地通知UILocalNotification

  转载自:http://www.2cto.com/kf/201403/285612.html 在去年做过一个小App,其中使用的关键功能就是向用户发送本地通知,可惜当时没有写博客的习惯,所以没有将对应的知识记录下来.最近又遇到了该功能的使用,这一次果断写个博客做下有关UILocalNotification的笔记. 首先是添加一个本地通知到系统中,代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2

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

这段时间项目要求做一个类似的闹钟提醒功能,对通知不太熟悉的我,决定先用到xcode自带的本地通知试试,最终成功的实现了功能,特整理分享下. 它的表现特点: app关闭的时候也能接收和显示通知. app处于后台的时候能接收通知也能显示. app处于前台的时候能接收,但不能显示,但是会走应用程序delegate中的方法 具体的创建方法: ->创建一个本地通知对象UILocalNotification ->设置fireDate,AlertBody,AlertAction,soundName,appl

iOS本地通知UILocalNotification

1.本地通知的定义和使用: 本地通知是UILocalNotification的实例,主要有三类属性: scheduled time,时间周期,用来指定iOS系统发送通知的日期和时间: notification type,通知类型,包括警告信息.动作按钮的标题.应用图标上的badge(数字标记)和播放的声音: 自定义数据,本地通知可以包含一个dictionary类型的本地数据. 对本地通知的数量限制,iOS最多允许最近本地通知数量是64个,超过限制的本地通知将被iOS忽略.  代码如下 复制代码

iOS本地通知:UILocalNotification

最近在做一个电商的APP,话说今年电商很火啊. 用到了本地通知,特此整理一下 添加一个本地通知到系统中,代码如下: // 初始化本地通知对象 UILocalNotification *notification = [[UILocalNotification alloc] init]; if (notification) { // 设置通知的提醒时间 NSDate *currentDate = [NSDate date]; notification.timeZone = [NSTimeZone d

iOS 浅谈本地通知 UILocalNotification

1.创建本地通知 UILocalNotification *local = [[UILocalNotification alloc] init]; 2.设置通知显示的时间 local.fireDate = [NSDate date]; 3.设置默认时区 local.timeZone = [NSTimeZone defaultTimeZone]; 4.设置提示内容 local.alertBody = @JPG下载完成,请即时查看!; 5.这个通知到时间时,你的应用程序右上角显示的数字. local

IOS 本地通知 UILocalNotification

1.增加一个本地推送 //设置20秒之后  NSDate *date = [NSDate dateWithTimeIntervalSinceNow:20];     //chuagjian一个本地推送     UILocalNotification *noti = [[[UILocalNotification alloc] init] autorelease];     if (noti) {         //设置推送时间         noti.fireDate = date;