iOS 8の革新の1つは、インタラクティブな通知です。 このような通知には、最大4つのアクションを割り当てることができます。それぞれのアクションは、バナーの下部にある通知テキスト付きの個別のボタンで表されます。 したがって、ユーザーは(通常)アプリケーションをアクティブにせずにメッセージに応答する追加の機会があります。 そのような機能のサポートを追加する方法を検討してください。
アクション定義
まず、通知に対して実行できるアクションを決定する必要があります。 これは、
UIMutableUserNotificationActionクラスのインスタンスを使用して行われます。
UIMutableUserNotificationAction *action_like = [[UIMutableUserNotificationAction alloc] init]; action_like.identifier = @"Like"; action_like.title = NSLocalizedString(@"Like",nil); action_like.activationMode = UIUserNotificationActivationModeBackground; action_like.destructive = NO; action_like.authenticationRequired = NO;
このクラスのインスタンスにはプロパティが含まれます。
identifier-アクションの文字列識別子。 このプロパティは、今後アクションを区別するために使用されます。
title-ボタンに表示されるタイトル。
activationMode-アクションをバックグラウンドで処理できるかどうか、またはアプリケーションをアクティブ化する必要があるかどうかを決定します。
destructive-アクションが破壊的かどうかを決定します。 その場合、このアクションのボタンが強調表示されます(通常は赤)。
authenticationRequired —アクションを完了するためにユーザー
認証が必要かどうかを決定します。
アプリケーションがアクティブ化される破壊的なアクションを決定する例:
UIMutableUserNotificationAction *action_del = [[UIMutableUserNotificationAction alloc] init]; action_del.identifier = @"Delete"; action_del.title = NSLocalizedString(@"Delete",nil); action_del.activationMode = UIUserNotificationActivationModeForeground; action_del.destructive = YES; action_del.authenticationRequired = NO;
カテゴリーの作成と通知処理パラメーターの登録
次に、
UIMutableUserNotificationCategoryクラスのインスタンスを使用して、通知のカテゴリを作成する必要があります。
UIMutableUserNotificationCategory *category= [[UIMutableUserNotificationCategory alloc] init]; category.identifier = @"my_category"; [category setActions:@[action_like, action_del] forContext:UIUserNotificationActionContextDefault]; NSSet *categories = [NSSet setWithObjects:category, nil];
カテゴリ識別子は、
pyaloadのプッシュ通知またはローカル通知の
カテゴリプロパティの
カテゴリキーとして送信する必要があり
ます 。
forContext引数は、通知に有効なアクションの数を設定します。
登録:
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:categories]; [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
通知の提出
次のフォームの通知を送信することにより:
{'aps': {'alert': 'A sample of interactive notification text', 'category': 'my_category', 'some_id': 'my_id' } }
(ペイロードのサイズの制限を考慮して、それほど長くない識別子で通知カテゴリを指定することは理にかなっています;しかし、これはトレーニングの例であるため、
my_categoryが使用されます)、次の形式の通知を受け取ります。
ロック画面で:

そしてシフト後:

通知領域で:

通知センター:

ボタンを押す操作
ボタンのクリックを処理するには、メソッドを定義する必要があります
- application:handleActionWithIdentifier:forLocalNotification:completionHandler:
ローカル通知を処理します。
そして
- application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
プッシュ通知を処理します。
これらのメソッドの2番目の引数はアクション識別子で、以前は次のように指定していました。
action_del.identifier = @"Delete";
最後の引数は、アクションの処理後
に呼び出す必要があるブロックです。
- (void) application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler { NSLog(@"NOTIFICATION IDENT: %@ USER INFO: %@", identifier, userInfo); completionHandler(); }
[
いいね ]ボタンをクリックしても、アプリケーションはアクティブになりません。 「
削除 」をクリックするのとは異なります。
おわりに
したがって、「手首のフリック」(c)を使用すると、アプリケーションに機能を追加して、ユーザーにとっての魅力を高めることができます(いずれにせよ、魅力を高めようとします)。