iOS下的Notification的使用
Notification是智能手機應(yīng)用編程中非常常用的一種傳遞信息的機制,而且可以非常好的節(jié)省資源屎鳍,不用消耗資源來不停地檢查信息狀態(tài)(Pooling)愿卒,在iOS下應(yīng)用分為兩種不同的Notification種類娘侍,本地和遠程向族。本地的Notification由iOS下NotificationManager統(tǒng)一管理,只需要將封裝好的本地Notification對象加入到系統(tǒng)Notification管理機制隊列中驶冒,系統(tǒng)會在指定的時間激發(fā)將本地Notification,應(yīng)用只需設(shè)計好處理Notification的方法就完成了整個Notification流程了韵卤。
本地Notification所使用的對象是UILocalNotification骗污,UILocalNotification的屬性涵蓋了所有處理Notification需要的內(nèi)容。UILocalNotification的屬性有fireDate沈条、timeZone需忿、repeatInterval、repeatCalendar蜡歹、alertBody屋厘、 alertAction、hasAction月而、alertLaunchImage汗洒、applicationIconBadgeNumber、 soundName和userInfo父款。
UILocalNotification的調(diào)度
其中fireDate溢谤、timeZone瞻凤、repeatInterval和repeatCalendar是用于UILocalNotification的調(diào)度。fireDate是UILocalNotification的激發(fā)的確切時間世杀。timeZone是UILocalNotification激發(fā)時間是否根據(jù)時區(qū)改變而改變阀参,如果設(shè)置為nil的話,那么UILocalNotification將在一段時候后被激發(fā)瞻坝,而不是某一個確切時間被激發(fā)蛛壳。 repeatInterval是UILocalNotification被重復(fù)激發(fā)之間的時間差,不過時間差是完全根據(jù)日歷單位(NSCalendarUnit)湿镀,例如每周激發(fā)的單位炕吸,NSWeekCalendarUnit,如果不設(shè)置的話勉痴,將不會重復(fù)激發(fā)赫模。 repeatCalendar是UILocalNotification重復(fù)激發(fā)所使用的日歷單位需要參考的日歷,如果不設(shè)置的話蒸矛,系統(tǒng)默認的日歷將被作為參考日歷瀑罗。
UILocalNotification的提醒內(nèi)容
alertBody、alertAction雏掠、hasAction和alertLaunchImage是當(dāng)應(yīng)用不在運行時斩祭,系統(tǒng)處理
UILocalNotification提醒是需要的內(nèi)容。alertBody是一串現(xiàn)實提醒內(nèi)容的字符串(NSString)乡话,如果 alertBody未設(shè)置的話摧玫,Notification被激發(fā)時將不現(xiàn)實提醒。alertAction也是一串字符(NSString)绑青,alertAction的內(nèi)容將作為提醒中動作按鈕上的文字诬像,如果未設(shè)置的話,提醒信息中的動作按鈕將顯示為“View”相對文字形式闸婴。alertLaunchImage是在用戶點擊提醒框中動作按鈕(“View”)時坏挠,等待應(yīng)用加載時顯示的圖片,這個將替代應(yīng)用原本設(shè)置的加載圖片邪乍。hasAction是一個控制是否在提醒框中顯示動作按鈕的布爾值降狠,默認值為YES。
UILocalNotification的其他部分
applicationIconBadgeNumber庇楞、soundName和userInfo將使UILocalNotification更完整榜配。applicationIconBadgeNumber是顯示在應(yīng)用圖標(biāo)右上角的數(shù)字,這樣讓用戶直接了解到應(yīng)用需要處理的 Notification吕晌。soundName是另一個UILocalNotification用來提醒用戶的手段芥牌,在Notification被激發(fā)之后將播放這段聲音來提醒用戶有Notification需要處理(有聲音設(shè)置時,聲音名稱要加上擴展名聂使,如sound1.mp3)壁拉,如果不設(shè)soundName的話谬俄,Notification被激發(fā)是將不會有聲音播放,除去應(yīng)用特制的聲音以外弃理,也可以將soundName設(shè)為UILocalNotificationDefaultSoundName來使用系統(tǒng)默認提醒聲音溃论。userInfo是Notification用來傳遞數(shù)據(jù)的NSDictionary。
登記UILocalNotification
在設(shè)置完UILocalNotification對象之后痘昌,應(yīng)用需要在系統(tǒng)Notification處理隊列中登記已設(shè)置完的UILocalNotification對象钥勋。登記UILocalNotification * localNotification的方式為:
[[UIApplication sharedApplication] ?scheduleLocalNotification:localNotification];
在有些時候,應(yīng)用可能需要直接激發(fā)一個Notification而不是等一段時間在激發(fā)辆苔,應(yīng)用可以以下的方式直接觸發(fā)已設(shè)好的Notification:
[[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
處理UILocalNotification
在提醒框動作按鈕被點擊后算灸,應(yīng)用開始運行時,可以在 -(BOOL)application:didFinishLaunchingWithOptions:這個Application delegate方法中處理驻啤》坡浚可以通過以下方式來加載為最近未處理的Notification:
UILocalNotification * localNotif=[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
如果應(yīng)用正在運行時,可以通過覆蓋在Application Delegate中的方法-(void)application:didReceiveLocalNotification:來處理 Notification骑冗。作為方法的第二個參數(shù)為UILocalNotification對象赊瞬,只需處理對象攜帶的userInfo來處理響應(yīng)的動作。
取消UILocalNotification
可以使用以下兩個方式來取消一個已經(jīng)登記的Notification贼涩,第一個方式可以直接取消一個指定的Notification巧涧,第二個方式將會把該應(yīng)用已登記的Notification一起取消
[[UIApplication sharedApplication] cancelLocalNotification:localNotification];
[[UIApplication sharedApplication] cancelAllLocalNotification];
一般需要應(yīng)用程序后臺運行時才會顯示提示,前臺運行時一般不顯示提示遥倦。如果想要當(dāng)應(yīng)用程序前臺應(yīng)行時也顯示提示谤绳,則可以通過將下面函數(shù)加到appDelegate中實現(xiàn):
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIApplicationState state = application.applicationState;
//????NSLog(@"%@,%d",notification,state);
if (state == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提醒"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"Close"
otherButtonTitles:@"OK",nil];
[alert show];
[alert release];
}
}
總結(jié)
本地Notification的機制在應(yīng)用開發(fā)中非常有效,可以很好的幫助開發(fā)者管理一些指定時間需要發(fā)生的事件袒哥,例如鬧鐘類的應(yīng)用缩筛。而且因為系統(tǒng)統(tǒng)一對Notification的管理,讓同樣的任務(wù)可以非常簡單得被處理统诺,而無需讓應(yīng)用浪費資源去等待事件的觸發(fā)。