アクション定義
まず、通知に対して実行できるアクションを決定する必要があります。 これは、 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)を使用すると、アプリケーションに機能を追加して、ユーザーにとっての魅力を高めることができます(いずれにせよ、魅力を高めようとします)。