LoginSignup
114
115

More than 5 years have passed since last update.

iOS8からのプッシュ通知

Last updated at Posted at 2014-08-27

iOS8からプッシュ通知の仕様が変わり、プッシュ通知アクション機能の追加などが行われました。
またそれにともなって、今まであった通常のプッシュ通知の登録、プッシュからアプリを開いた時のコールバックのメソッドが変更になりましたので、軽くまとめてみました。

通常のPush通知

APNSサーバーへの登録

iOS7まで

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

iOS8から

UIUserNotificationType types = UIUserNotificationTypeBadge |  UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];

[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];


通知の種類を表現する UIRemoteNotificationTypeUIUserNotificationType に変わったので注意が必要です。

iOS7以前と8移行の両立

これらのクラス、メソッドはiOS8移行でしか機能しないものなので、iOS7から8への移行期は両方のiOSに対応する必要があると思われます。
以下のように、iOSのバージョンごとにプッシュ通知の登録部分を分けて登録することでiOS7,8共に問題なく機能します。

if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] == NSOrderedAscending)
{
    /// iOS7 以前のプッシュ通知の登録
} else {
    /// iOS8 移行のプッシュ通知の登録
}

インタラクティブプッシュ通知

通知アクション

iOS8から新規に追加された機能の一つに通知アクションというものがあります。
プッシュ通知を左にスワイプするとボタンが表示され、ボタンごとにバックグラウンドで処理を行ったり、特定の画面を表示するなどできます
ロック画面・通知センターでは2つ、アラートの通知では4つまでアクションを用意する事ができます。
例えば、新規メール通知に対して、返信、アーカイブ、転送、削除のアクションが用意でき、削除、アーカイブはアプリを開かずに行えます

UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];

acceptAction.identifier = @"REPLY_ACTION";
acceptAction.title = @"返信";

background acceptAction.activationMode = UIUserNotificationActivationModeForeground;
acceptAction.destructive = NO;

device acceptAction.authenticationRequired = NO;
プロパティ 用途
identifier アクションの識別子(一意の値)
title プッシュ通知をスワイプした時に表示されるアクションの名前
activationMode ボタンが押された時にバックグラウンドで実行するか、アプリを開くか
destructive メールの削除等の破壊的な操作かどうか
authenticationRequired バックグラウンドのアクションの場合、ストレージへの書き込みなどデバイスのロック解除が必要かどうか

通知カテゴリ

一つのアプリでも複数種類のプッシュ通知が存在することが考えられます。
その時に通知アクションのまとまりを通知カテゴリとしてまとめて登録しておき、プッシュ通知のアクションはそのカテゴリに王子て表示されるという仕様になっています。

UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];

inviteCategory.identifier = @"NEW_MAIL_CATEGORY";

# アラートの通知の場合
[inviteCategory setActions:@[replyAction, deleteAction, archiveAction]
forContext:UIUserNotificationActionContextDefault];

# 通知センター、ロック画面の場合(2こしかひょうじされないため)
[inviteCategory setActions:@[replyAction, deleteAction]    forContext:UIUserNotificationActionContextMinimal];
114
115
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
114
115