忍者ブログ
  • 2024.04
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 2024.06
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

【2024/05/19 14:43 】 |
iOS通知のカスタムアクション実装
公式ドキュメント

を参照。
26ページから(特に28ページ冒頭)を要チェック。

引用----------

リスト 2-7 通知アクションを定義するコード例
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
// Define an ID string to be passed back to your app when you handle the action
acceptAction.identifier = @"ACCEPT_IDENTIFIER";
// Localized string displayed in the action button
acceptAction.title = @"Accept";
// If you need to show UI, choose foreground
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
// Destructive actions display in red
acceptAction.destructive = NO;
// Set whether the action requires the user to authenticate
acceptAction.authenticationRequired = NO;



リスト 2-8 アクションをカテゴリにまとめるコード例

// First create the category
UIMutableUserNotificationCategory *inviteCategory = [[UIMutableUserNotificationCategory alloc] init];
// Identifier to include in your push payload and local notification
inviteCategory.identifier = @"INVITE_CATEGORY";
// Add the actions to the category and set the action context
[inviteCategory setActions:@[acceptAction, maybeAction, declineAction] forContext:UIUserNotificationActionContextDefault];
// Set the actions to present in a minimal context
[inviteCategory setActions:@[acceptAction, declineAction]
forContext:UIUserNotificationActionContextMinimal];




リスト 2-9 通知カテゴリを登録するコード例

NSSet *categories = [NSSet setWithObjects:inviteCategory, alarmCategory, ...
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:types categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];



リスト 2-11 ローカル通知に用いるアクションのカテゴリを定義するコード例

UILocalNotification *notification = [[UILocalNotification alloc] init];
...
notification.category = @"INVITE_CATEGORY";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];




引用ここまで-------------


つまり

まずアクションを作成する。
UIUserNotificationActionクラス

identifier
title
activationMode
authenticationRequired
destructive

などの設定をする。
通知において、ひとつのボタンの操作に対応。


次に、アクションをまとめてカテゴリーを作る。
UIMutableUserNotificationCategory

カテゴリーはアクションの配列を格納し、識別子を持つ。
ひとつの通知におけるビューがこれで作られる。
カテゴリーは別に1アプリ1つではない。通知の種類によっていくらでもカテゴリーは作れる。
ただし、コンテキストによって2種類の配列を登録する必要がある。
UIUserNotificationActionContextDefault …これはモーダルビューで見た時
UIUserNotificationActionContextMinimal …これはロック画面で見た時。こちらは最大2つしかアクションを持てない



さらに、カテゴリーをセッティングにまとめる。
UIUserNotificationSettings
セッティングは、通知種別(アラート、バッジ、サウンドなど)を持つ


そして、このセッティングをアプリケーションに登録する。
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

これにて通知が仕えるようになる。



実際に使うためには、通知をスケジューリング登録する。

UILocalNotification *notification = [[UILocalNotification alloc] init];
...
notification.category = @"INVITE_CATEGORY";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];


PR
【2014/12/05 18:25 】 | iPhone | 有り難いご意見(0)
<<Xcodeでメモリ周りのデバッグ | ホーム | コードから制約を生成する>>
有り難いご意見
貴重なご意見の投稿














<<前ページ | ホーム | 次ページ>>