iOS 8 以前的遠端通知想必大家都很熟悉,這裡不再贅述,直接進入 iOS 8 新增的互動式遠端通知——讓使用者「不開 App」就能直接在通知上按下按鈕回覆。
透過 [[UIApplication sharedApplication] registerForRemoteNotifications] 完成註冊,並在下列兩個方法中處理結果:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
互動式通知的核心是「動作(Action)」與「類別(Category)」兩個概念:Action 描述一個按鈕的行為,Category 則把一組 Action 綁在一起,推送時透過 payload 指定要用哪一組。
UIMutableUserNotificationAction *cancelAction = [[UIMutableUserNotificationAction alloc] init]; [cancelAction setIdentifier:@"CancelNotificationActionIdentifier"]; [cancelAction setTitle:@"Cancel"]; [cancelAction setActivationMode:UIUserNotificationActivationModeBackground]; [cancelAction setAuthenticationRequired:YES]; [cancelAction setDestructive:YES];
identifier:該 action 的唯一識別碼,回呼時就是靠它分辨使用者按了哪個按鈕。
title:按鈕上顯示的文字。
activationMode:
UIUserNotificationActivationModeForeground:啟動 App 並切到前景展示。
UIUserNotificationActivationModeBackground:在背景啟動 App。實測發現,若 App 尚未啟動(不在背景也沒開啟),執行此模式的操作時 App 不會被開啟;若 App 已在前景,執行後仍停留在前景。
authenticationRequired:設為 YES 時,執行該操作前必須先解鎖裝置;設為 NO 則無需解鎖。若 activationMode 是 Foreground,這個值會被強制當作 YES 處理。實測結果:使用者裝置有密碼時,鎖屏狀態下設為 YES 需要輸入密碼,設為 NO 則不需要;若裝置本身沒設密碼,無論怎麼設都不需要輸入。
destructive:決定該按鈕是否以紅色顯示。只有在鎖屏以及從通知中心向左滑動時才有這種強調效果,頂部橫幅展示時不會出現。
UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
[notificationCategory setIdentifier:@"NotificationCategoryIdentifier"];
[notificationCategory setActions:@[acceptAction, cancelAction]
forContext:UIUserNotificationActionContextDefault];identifier:category 的唯一識別碼,其值必須與 payload 中的 category 欄位完全一致,否則通知不會顯示任何按鈕。
actions:UIUserNotificationAction 陣列。若設為 nil,則不會顯示任何操作按鈕。
context:
UIUserNotificationActionContextDefault:預設情境,可指定最多 4 個自訂操作。
UIUserNotificationActionContextMinimal:精簡情境,可指定最多 2 個自訂操作(適合鎖屏等空間有限的場合)。
UIUserNotificationType notificationTypes = (UIUserNotificationTypeAlert |
UIUserNotificationTypeSound |
UIUserNotificationTypeBadge);
NSSet *categoriesSet = [NSSet setWithObject:notificationCategory];
UIUserNotificationSettings *notificationSettings =
[UIUserNotificationSettings settingsForTypes:notificationTypes
categories:categoriesSet];把通知支援的類型與 category 綁在一起。這裡沒什麼難點,但要注意 categories 是一個 Set,代表可以一次註冊多組 category。
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
在 iOS 8 中透過這個方式註冊,並可依系統版本做區分處理(iOS 7 及更早仍走 registerForRemoteNotificationTypes:)。
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler {
if ([identifier isEqualToString:@"CancelNotificationActionIdentifier"]) {
NSLog(@"You chose cancel action.");
} else if ([identifier isEqualToString:@"AcceptNotificationActionIdentifier"]) {
NSLog(@"You chose accept action.");
}
if (completionHandler) {
completionHandler();
}
}參數說明:
application:收到通知的應用程式物件。
identifier:被觸發的 UIUserNotificationAction 的唯一識別碼。
userInfo:payload 的內容。
completionHandler:執行完指定操作後,必須在最後呼叫這個方法,否則系統會認為處理超時。
{
aps = {
alert = "Thank you very much";
badge = 1;
category = NotificationCategoryIdentifier;
sound = "ping.caf";
};
}要特別注意的是 category 這個欄位——如果 payload 裡沒有帶,或是它的值與你註冊的 identifier 不一致,通知一樣會正常顯示,但下面不會出現任何操作按鈕。這是這個功能最常見的除錯陷阱。
另外,本地通知(UILocalNotification)也支援同樣的互動機制,只要在建置時設定 category 屬性即可,回呼則走 handleActionWithIdentifier:forLocalNotification:completionHandler:。