top of page
  • isana.net

iOS8のInteractive Notifications

更新日:2019年12月24日


Interactive Notificationsとは?

iOS8では今までのNotificationに少し機能が追加されて、 受信したNotificationに対してアプリでアクションを設定できるようになります。 isana.netではOneSpeakというPush通知サービスを運営しており、OneSpeakの機能追加のためにInteractive Notificationsについて調査してみました。


従来はNotificationを受信した場合にはNotificationからアプリケーションを起動してから ユーザーに何らかのアクションをしてもらう必要がありましたが、 アクションのタイプによってはアプリケーションを起動する必要がなくなり、ユーザービリティの向上に繋がります。

Appleからの発表の画像の例だと、Facebookの「Like」がNotificationからできるようになります。 (投稿の内容をアプリで確認してからでないと「イイね」しづらいよね、というツッコミはあります。)

ボタンのタイトルはアプリから設定することができます。 使用中のアプリを離れること無くNotificationからアクションできるのは便利ですよね。


Interactive Notificationsの実装方法

対応するためには ・Notification受信側のiOSアプリ ・Notification送信側のサーバーサイド 両方に実装が必要です。

ではiOSアプリの実装から見てみます。 必要な実装は以下の通りです。

1.Notificationに表示するアクションを生成する 2.生成したアクションをカテゴリに設定する 3.生成したカテゴリをUIUserNotificationSettingsに設定する 4.設定したUIUserNotificationSettingsをNotificationに登録する

Notificationの内容を「承認」「拒否」するケースを例に実装してみます。

//1.Notificationに表示するアクションを生成する

// 「承認」の選択肢のアクション UIMutableUserNotificationAction *accept = [[UIMutableUserNotificationAction alloc] init]; accept.identifier = @”ACCEPT”;

//ボタン文字の設定 accept.title = @”承認”;

//選択後のアプリの挙動:Backgroundで選択結果を処理 accept.activationMode = UIUserNotificationActivationModeBackground;

// 選択ボタンの強調表示 デフォルトでNO Notification Centerでボタンが青表示になる accept.destructive = NO;

// 「拒否」の選択肢のアクション UIMutableUserNotificationAction *refuse = [[UIMutableUserNotificationAction alloc] init]; refuse.identifier = @”REFUSE”;

refuse.title = @”拒否”;

//選択後のアプリの挙動:アプリがForegroundに遷移して選択結果を処理 refuse.activationMode = UIUserNotificationActivationModeForeground;

// 選択ボタンの強調表示 この場合はNotification Centerでボタンが赤くなる refuse.destructive = YES;

//2.生成したアクションをカテゴリに設定する UIMutableUserNotificationCategory *marryme = [[UIMutableUserNotificationCategory alloc] init];

marryme.identifier = @”MARRY_ME”; [marryme setActions:@[accept, refuse] forContext:UIUserNotificationActionContextMinimal];

//3.生成したカテゴリをUIUserNotificationSettingsに設定する UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; NSSet *categories = [NSSet setWithObject:marryme]; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

//4.設定したUIUserNotificationSettingsをNotificationに登録する [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

 

続いてNotification送信側のサーバーサイドの実装です。 Notification送信時にcategoryの指定を行います。 (iOSアプリ側で実装したUIMutableUserNotificationCategoryのidentifierと合わせる)

{ “aps” : { “alert” : “ぼくと結婚してください!”, “category” : “MARRY_ME” } }

 

最後にiOSアプリ側でNotificationからアクションを選択された場合の処理の実装です。 iOS8からは以下のdelegate methodが追加されました。 application:handleActionWithIdentifier:forRemoteNotification:completionHandler:

– (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler {

//各アクションに設定したidentifierで判定する if ([identifier isEqualToString:@”ACCEPT”]) { // “承認”した時の処理 NSLog(@”承認をえらんだね”); } if ([identifier isEqualToString:@”REFUSE”]) { // 拒否した時の処理 NSLog(@”拒否をえらんだね”); }

// You must call this block at the end of your method. completionHandler();

}

 

以上で必要な実装が完了しました。

このNotificationの場合には「ぼくと結婚して下さい!」のメッセージに「承認」と「拒否」ボタンが表示されます。 「承認」を選択した時はBackgroundで動作するように実装していますのでアプリケーションが起動しなくても承認用の処理が行われます。

弊社のOneSpeakにもInteractive Notificationsを実装する予定です。

bottom of page