標籤:http io ar os 使用 for 檔案 資料 on
這兩天在做一個議程提醒功能,用到了本地通知的功能,記錄相關知識如下:
1、本地通知的定義和使用:
本地通知是UILocalNotification的執行個體,主要有三類屬性:
scheduled time,時間周期,用來指定iOS系統發送通知的日期和時間;
notification type,通知類型,包括警告資訊、動作按鈕的標題、應用表徵圖上的badge(數字標記)和播放的聲音;
自訂資料,本地通知可以包含一個dictionary類型的本機資料。
對本地通知的數量限制,iOS最多允許最近本地通知數量是64個,超過限制的本地通知將被iOS忽略。
代碼如下 複製代碼
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) {
return;
}
//設定本地通知的觸發時間(如果要立即觸發,無需設定),這裡設定為20妙後
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];
//設定本地通知的時區
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//設定通知的內容
localNotification.alertBody = affair.title;
//設定通知動作按鈕的標題
localNotification.alertAction = @"查看”;
//設定提醒的聲音,可以自己添加音效檔,這裡設定為預設提示聲
localNotification.soundName = UILocalNotificationDefaultSoundName;
//設定通知的相關資訊,這個很重要,可以添加一些標記性內容,方便以後區分和擷取通知的資訊
NSDictionary *infoDic = [NSDictionary dictionaryWithObjectsAndKeys:LOCAL_NOTIFY_SCHEDULE_ID,@"id",[NSNumber numberWithInteger:time],@"time",[NSNumber numberWithInt:affair.aid],@"affair.aid", nil];
localNotification.userInfo = infoDic;
//在規定的日期觸發通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
//立即觸發一個通知
// [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
[localNotification release];
2、取消本地通知:
代碼如下 複製代碼
//取消某一個通知
NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];
//擷取當前所有的本地通知
if (!notificaitons || notificaitons.count <= 0) {
return;(www.111cn.net)
}
for (UILocalNotification *notify in notificaitons) {
if ([[notify.userInfo objectForKey:@"id"] isEqualToString:LOCAL_NOTIFY_SCHEDULE_ID]) {
//取消一個特定的通知
[[UIApplication sharedApplication] cancelLocalNotification:notify];
break;
}
}
//取消所有的本地通知
[[UIApplication sharedApplication] cancelAllLocalNotifications];
3、本地通知的響應:
如果已經註冊了本地通知,當用戶端響應通知時:
a、應用程式在背景時候,本地通知會給裝置送達一個和遠程通知一樣的提醒,提醒的樣式由使用者在手機設定中設定
b、應用程式正在運行中,則裝置不會收到提醒,但是會走應用程式delegate中的方法:
代碼如下 複製代碼
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
}
,如果你想實現程式在後台時候的那種提醒效果,可以在上面這個方法中添加相關代碼,範例程式碼:
代碼如下 複製代碼
if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"關閉" otherButtonTitles:notification.alertAction, nil nil];
[alert show];
}
需要注意的是,在情況a中,如果使用者點擊提醒進入應用程式,也會執行收到本地通知的回調方法,這種情況下如果你添加了上面那段代碼,則會出現連續出現兩次提示,為瞭解決這個問題,修改代碼如下:
代碼如下 複製代碼
if ([[notification.userInfo objectForKey:@"id"] isEqualToString:@"affair.schedule"]) {
//判斷應用程式當前的運行狀態,如果是啟用狀態,則進行提醒,否則不提醒
if (application.applicationState == UIApplicationStateActive) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"test" message:notification.alertBody delegate:nil cancelButtonTitle:@"關閉" otherButtonTitles:notification.alertAction, nil nil];
[alert show];
}
}
from:http://www.111cn.net/sj/iOS/60372.htm
Ios開發中UILocalNotification實現本地通知實現提醒功能