UserNotifications Framework の Custom Action 実装方法
iOS10 から Notification がカスタマイズできるようになりました。
基本的に Notification の実装方法は Local and Remote Notification Programming Guide の通りなのですが、カスタマイズのやり方で一部記載されていない点があったのでメモ。
Custom Action を表示するために
Custom Action を表示するためには、
1.Custom Action のカテゴリID
2.表示するコンテンツのカテゴリID
を同一にする必要があるようです。
(同一でない場合は Custom Action のない通知が表示されました)
以下ソースコードになります。
環境 : Xcode 8.2.1 (iOS SDK 10.2) / シュミレータ
// ViewController.h #import <UIKit/UIKit.h> #import <UserNotifications/UserNotifications.h> @interface ViewController : UIViewController <UNUserNotificationCenterDelegate> @end
// ViewController.m #import "ViewController.h" @interface ViewController () @end /** カテゴリID */ static NSString *const CSKCategoryIdentifier = @"TEST_ACTIONS"; @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%s", __func__); // Notification のパーミッション許可要求 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // Notification のパーミッションが有効 } }]; // Delegate を設定 [UNUserNotificationCenter currentNotificationCenter].delegate = self; } - (IBAction)registerLocalNotification:(UIButton *)sender { // カスタム Action を設定 UNNotificationAction* noneAction = [UNNotificationAction actionWithIdentifier:@"NONE_ACTION" title:@"None" options:UNNotificationActionOptionNone]; UNNotificationAction* destructiveAction = [UNNotificationAction actionWithIdentifier:@"DESTRUCTIVE_ACTION" title:@"Destructive" options:UNNotificationActionOptionDestructive]; UNNotificationCategory* category = [UNNotificationCategory // 1.Custom Action のカテゴリID categoryWithIdentifier:CSKCategoryIdentifier actions:@[noneAction, destructiveAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone]; // NotificationCenter に カテゴリ を登録 UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter]; [center setNotificationCategories:[NSSet setWithObjects:category, nil]]; // 通知するメッセージを設定 UNMutableNotificationContent* content = [UNMutableNotificationContent new]; content.title = [NSString localizedUserNotificationStringForKey:@"Title" arguments:nil]; content.body = [NSString localizedUserNotificationStringForKey:@"Body" arguments:nil]; content.subtitle = [NSString localizedUserNotificationStringForKey:@"Subtitle" arguments:nil]; content.sound = [UNNotificationSound defaultSound]; // 2.表示するコンテンツのカテゴリID content.categoryIdentifier = CSKCategoryIdentifier; // 5秒後に通知するようにトリガーを設定 UNTimeIntervalNotificationTrigger* trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:5 repeats:NO]; UNNotificationRequest* request = [UNNotificationRequest requestWithIdentifier:@"Wait" content:content trigger:trigger]; // Notification のリクエストを追加 [center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) { }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
で以下のようなカスタムアクションが表示されます。