環境
- Xcode 6 beta 6
- iOS 8 beta 5
- Parse
Push 通知の設定
[[UIApplication sharedApplication] registerForRemoteNotifications]; // <- Push 通知の利用登録(デバイストークンの発行)UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings]; // <- 利用許可を求めるアラートの表示
選択肢の設定
UIMutableUserNotificationAction *acceptAction = [UIMutableUserNotificationAction new];acceptAction.identifier = @"ACCEPT_IDENTIFIER";acceptAction.title = @"Accept";acceptAction.activationMode = UIUserNotificationActivationModeBackground;acceptAction.destructive = NO;acceptAction.authenticationRequired = NO;UIMutableUserNotificationAction *maybeAction = [UIMutableUserNotificationAction new];maybeAction.identifier = @"MAYBE_IDENTIFIER";maybeAction.title = @"Maybe";maybeAction.activationMode = UIUserNotificationActivationModeBackground;maybeAction.destructive = NO;maybeAction.authenticationRequired = NO;UIMutableUserNotificationAction *declineAction = [UIMutableUserNotificationAction new];declineAction.identifier = @"DECLINE_IDENTIFIER";declineAction.title = @"Decline";declineAction.activationMode = UIUserNotificationActivationModeBackground;declineAction.destructive = YES;declineAction.authenticationRequired = NO;UIMutableUserNotificationCategory *inviteCategory = [UIMutableUserNotificationCategory new];inviteCategory.identifier = @"INVITE_CATEGORY";[inviteCategory setActions:@[acceptAction, maybeAction, declineAction] forContext:UIUserNotificationActionContextDefault];[inviteCategory setActions:@[acceptAction, declineAction] forContext:UIUserNotificationActionContextMinimal];NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories];[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
- identifier
- アクションを表す識別子
- title
- バナーやアラートに表示されるボタンタイトル
- activationMode
- Background、または Foreground を指定
- Background であればアプリが起動することなく処理され、Foreground であればアプリが起動されます
- destructive
- ボタンタイトルを強調するかどうか
- 削除など取り返しのつかない処理などで YES にする
- authenticationRequired
- YES を指定すると選択時にパスコードをの入力を求める
- (iOS 8 beta 5 の時点では YES/NO に関わらず動きの違いはありませんでした。)
- UIUserNotificationActionContextDefault
- ダイアログ表示時
- 表示可能なアクションは最大 4 個
- UIUserNotificationActionContextMinimal
- バナー表示時
- 表示可能なアクションは最大 2 個
選択肢付き Push 通知を送る
{"aps": {"alert": "Invitation from Jane Appleseed","category": "INVITE_CATEGORY"}}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {if ([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]) {// ...} else if ([identifier isEqualToString:@"MAYBE_IDENTIFIER"]) {// ...}if (completionHandler) {completionHandler();}}
おまけ
UILocalNotification *localNotification = [UILocalNotification new];localNotification.alertBody = @"Local notification";localNotification.category = @"INVITE_CATEGORY";[application scheduleLocalNotification:localNotification];
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler {if ([identifier isEqualToString:@"ACCEPT_IDENTIFIER"]) {// ...} else if ([identifier isEqualToString:@"MAYBE_IDENTIFIER"]) {// ...}if (completionHandler) {completionHandler();}}
まとめ
https://gist.github.com/qmihara/8f39025e9857b498047f
参考情報
iOS Dev Center
UIUserNotificationCategory
UIMutableUserNotificationCategory
UIUserNotificationAction
UIMutableUserNotificationAction