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
回答2件
あなたの回答
tips
プレビュー