UILocalNotification的使用

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。


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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章