質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

解決済

2回答

2271閲覧

iOS 通知を削除した時の検知方法について

hobby2018

総合スコア22

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2021/03/11 03:56

編集2021/03/12 03:00

iOSでユーザーが通知を削除したタイミング検知をしたいです。
以下のように実装しておりますが、消去した時に

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)

このメソッドに到達しません。
質問内容と致しましては、通知削除を捕捉することは可能なのか?
また、可能な場合にはコードの問題点などをご指摘頂けると幸いです。

参考にしたサイト

よろしくお願い致します。

追記
ここでの通知の削除は、左スワイプして削除ボタンを押した場合を想定
しております。

swift

1import UIKit 2import UserNotifications 3 4@main 5class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 6 7 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 8 // Override point for customization after application launch. 9 10 let center = UNUserNotificationCenter.current() 11 center.delegate = self 12 let delete = UNNotificationAction(identifier:"deleteActionIdentifier", 13 title:"Delete", 14 options:[.destructive, .authenticationRequired]) 15 let foreground = UNNotificationAction(identifier:"replyActionIdentifier", 16 title:"Reply", 17 options:[.foreground]) 18 let category = UNNotificationCategory(identifier: UNNotificationDismissActionIdentifier, actions: [delete, foreground], intentIdentifiers: [], options: .customDismissAction) 19 center.setNotificationCategories([category]) 20 21 center.requestAuthorization(options: [.badge, .alert], completionHandler: { (granted, error) in 22 if error != nil { 23 return 24 } 25 26 if granted { 27 debugPrint("通知許可") 28 } else { 29 debugPrint("通知拒否") 30 } 31 }) 32 33 return true 34 } 35 36 // MARK: UISceneSession Lifecycle 37 38 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 39 // Called when a new scene session is being created. 40 // Use this method to select a configuration to create the new scene with. 41 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 42 } 43 44 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 45 // Called when the user discards a scene session. 46 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 47 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 48 } 49 50 // フォアグラウンドで通知受信 51 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 52 completionHandler([.alert, .sound]) 53 } 54 55 func userNotificationCenter(_ center: UNUserNotificationCenter, 56 didReceive response: UNNotificationResponse, 57 withCompletionHandler completionHandler: () -> Swift.Void) { 58 // DismissAction かどうかを判別 59 if response.actionIdentifier == UNNotificationDismissActionIdentifier { 60 print("Dismiss Action") 61 } 62 63 completionHandler() 64 } 65 66} 67

swift

1import UIKit 2 3class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view. 8 9 let content = UNMutableNotificationContent() 10 content.title = "Title" 11 content.subtitle = "Subtitle" 12 content.body = "Body" 13 content.sound = UNNotificationSound.default 14 15 // 5秒後に発火 16 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 17 let request = UNNotificationRequest(identifier: UNNotificationDismissActionIdentifier, 18 content: content, 19 trigger: trigger) 20 // ローカル通知予約 21 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 22 } 23 24} 25

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

CHERRY

2021/03/12 00:10

参考にされた URL に > 通知を削除した時のイベントをハンドリングする > システムが提供する通知 UI 上の「Dismiss」または「Clear」または「x」ボタンを押すことで、通知を削除するアクションを実行できます。このアクションが実行されたタイミングで、前述したメソッドが呼ばれます。 とありますが、このアクションをしても 呼ばれないということでしょうか?
hobby2018

2021/03/12 02:55

はい、そうです。 AppDelegateとViewController丸々貼り付けておりますので 再現もすぐできます。
guest

回答2

0

自己解決

通知の削除のタイミングを捕捉するためには、UNNotificationCategoryの作成時に指定したidentifier
と同じものをUNMutableNotificationContentcategoryIdentifierにセットする必要がありました。

両者を紐付けることで、.customDismissActionに対応したUNNotificationDismissActionIdentifierの値をもつresponseが

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Swift.Void)

にて無事呼ばれました。

let category = UNNotificationCategory(identifier: "categoryId", actions: [], intentIdentifiers: [], options: .customDismissAction) let content = UNMutableNotificationContent() // カテゴリの identifier をセットする content.categoryIdentifier = "categoryId"

最終的なソースコード

AppDelegate

1import UIKit 2import UserNotifications 3 4@main 5class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 6 7 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 8 // Override point for customization after application launch. 9 10 let center = UNUserNotificationCenter.current() 11 center.delegate = self 12 let category = UNNotificationCategory(identifier: "categoryId", actions: [], intentIdentifiers: [], options: .customDismissAction) 13 center.setNotificationCategories([category]) 14 15 center.requestAuthorization(options: [.badge, .alert], completionHandler: { (granted, error) in 16 if error != nil { 17 return 18 } 19 20 if granted { 21 debugPrint("通知許可") 22 } else { 23 debugPrint("通知拒否") 24 } 25 }) 26 27 return true 28 } 29 30 // MARK: UISceneSession Lifecycle 31 32 func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 33 // Called when a new scene session is being created. 34 // Use this method to select a configuration to create the new scene with. 35 return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 36 } 37 38 func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) { 39 // Called when the user discards a scene session. 40 // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 41 // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 42 } 43 44 // フォアグラウンドで通知受信 45 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { 46 completionHandler([.alert, .sound]) 47 } 48 49 func userNotificationCenter(_ center: UNUserNotificationCenter, 50 didReceive response: UNNotificationResponse, 51 withCompletionHandler completionHandler: () -> Swift.Void) { 52 // DismissAction かどうかを判別 53 if response.actionIdentifier == UNNotificationDismissActionIdentifier { 54 print("Dismiss Action") 55 } 56 57 completionHandler() 58 } 59 60} 61

ViewController

1import UIKit 2 3class ViewController: UIViewController { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 // Do any additional setup after loading the view. 8 9 let content = UNMutableNotificationContent() 10 content.title = "Title" 11 content.subtitle = "Subtitle" 12 content.body = "Body" 13 content.sound = UNNotificationSound.default 14 15 // カテゴリの identifier をセットする 16 content.categoryIdentifier = "categoryId" 17 18 // 5秒後に発火 19 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) 20 let request = UNNotificationRequest(identifier: "requestId", 21 content: content, 22 trigger: trigger) 23 // ローカル通知予約 24 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 25 } 26 27}

投稿2021/03/12 07:54

hobby2018

総合スコア22

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

「iOSでユーザーが通知を削除したタイミング」とのことですが、「通知を削除」の定義が曖昧かと思いました。

通知UIを画面上にスワイプした場合や、左スワイプして削除ボタンを押した場合等によって検知できる条件が異なります。
詳しくは 公式 を見て、できること / できないことを整理してみることをお勧めします。

投稿2021/03/11 23:23

errolizer

総合スコア441

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問