UILocalNotification的使用

来源:互联网 发布:centos server docker 编辑:程序博客网 时间:2024/05/22 08:28

Notification是智能手机应用编程中非常常用的一种传递信息的机制,而且可以非常好的节省资源,不用消耗资源来不停地检查信息状态(Pooling),在iOS下应用分为两种不同的Notification种类,本地和远程。本地的Notification由iOS下NotificationManager统一管理,只需要将封装好的本地Notification对象加入到系统Notification管理机制队列中,系统会在指定的时间激发将本地Notification,应用只需设计好处理Notification的方法就完成了整个Notification流程了。

本地Notification所使用的对象是UILocalNotificationUILocalNotification的属性涵盖了所有处理Notification需要的内容。UILocalNotification的属性有fireDate、timeZone、repeatInterval、repeatCalendar、alertBody、alertAction、hasAction、alertLaunchImage、applicationIconBadgeNumber、soundName和userInfo。

@property(nonatomic,copy)NSDate *fireDate;  //通知 触发的时间

@property(nonatomic,copy)NSTimeZone *timeZone;  // 通知所在时区,如果指定时区,可以自行调整时间    

@property(nonatomic)NSCalendarUnit repeatInterval;    // 重复间隔

@property(nonatomic,copy)NSCalendar *repeatCalendar;// 重复日历

@property(nonatomic,copy)NSString *alertBody; // 通知的正文

@property(nonatomic)BOOL hasAction;  

@property(nonatomic,copy)NSString *alertAction; //

@property(nonatomic,copy)NSString *alertLaunchImage;  

@property(nonatomic,copy)NSString *soundName; 

//通知播放的声音,如果用 UILocalNotificationDefaultSoundName 使用的系统默认

@property(nonatomic)NSInteger applicationIconBadgeNumber;  // 应用程序图标

@property(nonatomic,copy)NSDictionary *userInfo;  // 数据字典

通过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的管理,让同样的任务可以非常简单得被处理,而无需让应用浪费资源去等待事件的触发。


#import "AppDelegate.h"


@implementation AppDelegate

/**

 提示:通知和应用是分别处理的,应用程序调度了通知之后,即便被关掉,仍然能够在指定的调度时间被触发。

 

 通常在使用本地通知时,是在应用程序退出到后台时来调度的。

 

 应用程序退出到后台之后,会在内存中驻留10分钟的时间,过了10分钟之后,系统会自动清理应用程序,

 被清理之后,再次运行时,才会调用application:didFinishLaunchingWithOptions:方法。

 

 1. 通常本地通知是在退出应用程序时,被调度的,在后台往往会做一些后台调度的服务,

 

 譬如

 1) 一个联网下棋的软件:

 当对手落子后,需要进行检测,检测完成后,通过本地通知的方式通知用户重新回到应用程序

 2) 例如一个新闻客户端,在后台检测最新的数据,当发现数据后,通知用户,用户进入应用时,直接显示通知的内容

 3)未接来电

 4QQ聊天

 

 如果要实现类似的上述功能,可以借助userInfo数据字典

 

 要获取到通知的消息,可以在application:didFinishLaunchingWithOptions:方法中实现

 

 

 

*/

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

  //截获本地通知,用户通过通知的横幅点示点入系统,字典中才能找到本地通知

    UILocalNotification *notification=[launchOptionsobjectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    

   //判断通知是否存在

   if (notification)

    {

       NSLog(@"%@",notification.userInfo);

    }

    

    

    returnYES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}


#pragma mark -  进入后台的时候调用

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    UILocalNotification *notification=[[UILocalNotificationalloc]init];

    //5秒之后触发

    notification.fireDate=[[NSDatealloc]initWithTimeIntervalSinceNow:5];

    notification.alertBody=@"最新消息";

    notification.soundName=UILocalNotificationDefaultSoundName;

    notification.applicationIconBadgeNumber=100;

    //要调用要地通知,需要通过UIApplication来统一调度

    [[UIApplicationsharedApplication]scheduleLocalNotification:notification];

    




}

#pragma mark -  进入前台时间调用

- (void)applicationWillEnterForeground:(UIApplication *)application

{

    //图标清0

    application.applicationIconBadgeNumber=0;

}


- (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.

}


- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


@end






原创粉丝点击