Local Notifications let users know it has something for them
Local notifications 是一種讓該 iPhone APP 沒有正在執行放置在背景時候,而讓使用者可以知道有事情要提醒。這提醒可以是一個訊息提示、可以是行事曆事件、新資料等等。當 iOS 呈現該提示時,顯示訊息可以是 Alert 訊息或者用 Badge 的方式加在 APP 的 icon 上,搭配著聲音提醒。而當使用者看到這樣的提醒,可以選擇關閉或者點選來看,點選看內容則會開啓該 APP,再由 APP 導向訊息位置。
Local Notifications 在 iOS 4.0 之後才支援。
想要讓 iOS 可以在稍候傳遞 local notification 出來,要使用 UILocalNotification,將它登記日期時間,顯示的細節,將它登記 schedules 起來即可。當 iOS 收到這樣訊息,會根據該配製來決定 (alert, icon badge number, sound) 三種組合的形式。而當使用者點選 action button,那麼 APP 會啓動呼叫 UIApplicationDelegate 的 application:didReceiveLocalNotification: 並且將 local notification 物件傳遞進去。所以我們即可從這邊接收到的 local notification 來決定之後的導引處理。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)openAlertAction { | |
UILocalNotification *localNotification = | |
[[UILocalNotification alloc] init]; | |
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow: | |
(NSTimeInterval) 30]; | |
localNotification.timeZone = [NSTimeZone defaultTimeZone]; | |
localNotification.alertBody = @"Reminder for Staff meeting in 30 mins."; | |
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys: | |
value1, key1, nil]; | |
localNotification.userInfo = userInfo; | |
localNotification.alertAction = NSLocalizedString(@"Detail", nil); | |
localNotification.soundName = UILocalNotificationDefaultSoundName; | |
localNotification.applicationIconBadgeNumber += 1; | |
[[UIApplication sharedApplication] scheduleLocalNotification: | |
localNotification]; | |
} |
如何解析該 Local Notification。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)application:(UIApplication *)application | |
didReceiveLocalNotification:(UILocalNotification *)notif { | |
TTDPRINT(@"Key1's value %@", [notif.userInfo objectForKey:Key1]); | |
application.applicationIconBadgeNumber = | |
notif.applicationIconBadgeNumber -1; | |
} |
我們在設計這邊時候,要注意存足夠資訊進入 localNotification.userInfo,這樣在未來接收到 localNotification.userInfo 才能有足夠資訊做後面邏輯處理,例如導引到開啓不同的 UIViewController 顯示。想要瞭解更多可以參考 UILocalNotification Class Reference.
Comments
Post a Comment