もともとは、iOS 標準の“メッセージ”アプリのみ利用可能だった通知からその場で返信機能。iOS 9 では、サードパーティのアプリでもAPIが開放され、上のスクリーンショットのように通知センターの通知からすぐ返信画面を表示することができるようになっています。
キーワードはこれ。UIUserNotificationActionBehaviorTextInput
。
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc] init];
replyAction.title = @"返信";
replyAction.activationMode = UIUserNotificationActivationModeBackground;
replyAction.authenticationRequired = YES;
replyAction.identifier = @"replyAction";
replyAction.behavior = UIUserNotificationActionBehaviorTextInput; // これです
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"category_reply";
[category setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
.
.
.
UIMutableUserNotificationAction
に新しいプロパティ behavior
が新たに追加されたんですね。
また、テキストを入力した後、“送信”ボタンが押された時の処理についてですが、例えばローカル通知の場合、(選択肢のタップなど)通知から何かアクションがあると以下の様なメソッドが実行されます。
AppDelegate.m
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
withResponseInfo:(NSDictionary *)responseInfo // 新たに追加
completionHandler:(void (^)())completionHandler {
// 処理
// ちなみに Remote Notification でも同様に withResponseInfo という引数が使える
}
このresponseInfo
にテキストが格納されています。テキストを取り出すには、
[responseInfo objectForKey:UIUserNotificationActionResponseTypedTextKey]
でテキストを取り出せます。
ちなみに:返信画面で、画面がロックされるまで放置すると、返信画面がまるまる消えてしまい、それまで入力した文章が全部失われるようです。文章構成をゆっくり考えていて、気づくと全部消えてしまったなんて悲劇(※実話)が……。
とはいえ、使う側にとっては楽な機能なのでメッセージ系のアプリはいち早く対応して欲しいですね。