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的管理,讓同樣的任務可以非常簡單得被處理,而無需讓應用浪費資源去等待事件的觸發。
轉自:http://hi.baidu.com/%CB%BC%C3%F4%D3%EA/blog/item/173ee25032b4d301367abe8a.html
以下為舉例:
///
//發送通知
UILocalNotification *notification=[[UILocalNotification alloc] init];
if (notification!=nil) {
NSDate *now=[NSDate new];
notification.fireDate=[now dateByAddingTimeInterval: 10]; // 10秒後通知
notification.repeatInterval= 0; // 循環次數,kCFCalendarUnitWeekday一周一次
notification.timeZone=[NSTimeZone defaultTimeZone];
notification.applicationIconBadgeNumber= 1; // 應用的紅色數字
notification.soundName= UILocalNotificationDefaultSoundName; // 聲音,可以換成alarm.soundName = @"myMusic.caf"
// 去掉下面2行就不會彈出提示框
notification.alertBody= @" 通知内容 "; // 提示資訊 彈出提示框
notification.alertAction = @" 打開 "; // 提示框按鈕
// notification.hasAction = NO; // 是否顯示額外的按鈕,為no時alertAction消失
// NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"];
// notification.userInfo = infoDict; // 添加額外的資訊
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
[notification release];

取消通知

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
application.applicationIconBadgeNumber = 0;
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
- ( void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// 點選提示框的打開
application.applicationIconBadgeNumber = 0;
}
- ( void)applicationDidBecomeActive:(UIApplication *)application {
// 當程式還在後天運作
application.applicationIconBadgeNumber = 0;
}
///
- 第一步:建立本地推送
- // 建立一個本地推送
- UILocalNotification *notification = [[[UILocalNotification alloc] init] autorelease];
- //設定10秒之後
- NSDate *pushDate = [NSDate dateWithTimeIntervalSinceNow:10];
- if (notification != nil) {
- // 設定推送時間
- notification.fireDate = pushDate;
- // 設定時區
- notification.timeZone = [NSTimeZone defaultTimeZone];
- // 設定重複間隔
- notification.repeatInterval = kCFCalendarUnitDay;
- // 推送聲音
- notification.soundName = UILocalNotificationDefaultSoundName;
- // 推送内容
- notification.alertBody = @"推送内容";
- //顯示在icon上的紅色圈中的數子
- notification.applicationIconBadgeNumber = 1;
- //設定userinfo 友善在之後需要撤銷的時候使用
- NSDictionary *info = [NSDictionary dictionaryWithObject:@"name"forKey:@"key"];
- notification.userInfo = info;
- //添加推送到UIApplication
- UIApplication *app = [UIApplication sharedApplication];
- [app scheduleLocalNotification:notification];
- }
- 第二步:接收本地推送
- - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iWeibo" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
- [alert show];
- // 圖示上的數字減1
- application.applicationIconBadgeNumber -= 1;
- }
- 第三步:解除本地推送
- // 獲得 UIApplication
- UIApplication *app = [UIApplication sharedApplication];
- //擷取本地推送數組
- NSArray *localArray = [app scheduledLocalNotifications];
- //聲明本地通知對象
- UILocalNotification *localNotification;
- if (localArray) {
- for (UILocalNotification *noti in localArray) {
- NSDictionary *dict = noti.userInfo;
- if (dict) {
- NSString *inKey = [dict objectForKey:@"key"];
- if ([inKey isEqualToString:@"對應的key值"]) {
- if (localNotification){
- [localNotification release];
- localNotification = nil;
- }
- localNotification = [noti retain];
- break;
- }
- }
- }
- //判斷是否找到已經存在的相同key的推送
- if (!localNotification) {
- //不存在初始化
- localNotification = [[UILocalNotification alloc] init];
- }
- if (localNotification) {
- //不推送 取消推送
- [app cancelLocalNotification:localNotification];
- [localNotification release];
- return;
- }
- }
///