前提・実現したいこと
日付を指定して任意のタイミングでローカル通知を受け取りたいのですが
通知を受け取るタイミングの指定を、日付指定にしたパターンの時のみ
通知が飛んで来ず原因がわからないため、ご教示頂きたいです。
こちらを参考にして、未発火の通知を出力してみたのですが
どうもそもそも通知が登録されてない(?)ようなのですが原因がわかりませんでした。
前提として、アプリ起動後にアプリはバックグラウンド状態にしています。
リンク先の、一度だけ通知・繰り返し通知のパターンでは通知が飛んでくることを確認しました。
日付を指定するカレンダー通知のパターンの時のみ
通知が飛んでくることを確認できていない状況です。動作確認は実機で行っております。
ご回答頂けると幸いです。宜しくお願い致します。
出力結果
許可する ==========Pending Notification============ []
該当のソースコード
import UIKit import UserNotifications @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. // アプリのバッジの更新、サウンド、アラートの通知に関してユーザへ確認 let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert, .badge, .sound]) {(granted, error) in if granted { print("許可する") } else { print("許可しない") } } return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
Swift
1import UIKit 2import UserNotifications 3 4class ViewController: UIViewController { 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 let calendar = Calendar.current 9 let date = Date() 10 let after = calendar.date(byAdding: .minute, value: 1, to: date)! 11 let dateComponents = Calendar.current.dateComponents(in: .current, from: after) 12 13 let content = UNMutableNotificationContent() 14 content.title = "カレンダー通知" 15 content.subtitle = "サブタイトル、これはiOS10から新登場" 16 content.body = "カレンダー通知が発火されました。" 17 content.sound = UNNotificationSound.default 18 19 // 指定した日付に発火 20 let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false) 21 let request = UNNotificationRequest(identifier: "notification", 22 content: content, 23 trigger: trigger) 24 25 // カレンダー通知予約 26 let center = UNUserNotificationCenter.current() 27 center.add(request, withCompletionHandler: nil) 28 29 center.getPendingNotificationRequests { (requests) in 30 print("==========Pending Notification============") 31 print(requests) 32 } 33 } 34}
###追記
(lldb) po dateComponents ▿ calendar: gregorian (current) timeZone: Asia/Tokyo (current) era: 1 year: 2019 month: 1 day: 30 hour: 22 minute: 52 second: 29 nanosecond: 591760039 weekday: 4 weekdayOrdinal: 5 quarter: 0 weekOfMonth: 5 weekOfYear: 5 yearForWeekOfYear: 2019 isLeapMonth: false ▿ calendar : gregorian (current) - identifier : Foundation.Calendar.Identifier.gregorian - kind : "current" ▿ locale : Optional<Locale> ▿ some : en_JP (current) - identifier : "en_JP" - kind : "current" ▿ timeZone : Asia/Tokyo (current) - identifier : "Asia/Tokyo" - kind : "current" ▿ abbreviation : Optional<String> - some : "JST" - secondsFromGMT : 32400 - isDaylightSavingTime : false - firstWeekday : 1 - minimumDaysInFirstWeek : 1 ▿ timeZone : Asia/Tokyo (current) - identifier : "Asia/Tokyo" - kind : "current" ▿ abbreviation : Optional<String> - some : "JST" - secondsFromGMT : 32400 - isDaylightSavingTime : false - era : 1 - year : 2019 - month : 1 - day : 30 - hour : 22 - minute : 52 - second : 29 - nanosecond : 591760039 - weekday : 4 - weekdayOrdinal : 5 - quarter : 0 - weekOfMonth : 5 - weekOfYear : 5 - yearForWeekOfYear : 2019 - isLeapMonth : false
###追記2
dateからdateComponentsに変換した場合とdateComponents決め打ちの場合で
両方デバッグ出力して比較してみましたが、dateからdateComponentsに変換した場合は
色々と付加情報がついていたので、憶測でしかないですがこの辺りが怪しい気がします。
//通知飛んで来ない //2019/1/30 23:05 実行 let calendar = Calendar.current let date = Date() let after = calendar.date(byAdding: .minute, value: 1, to: date)! let dateComponents = Calendar.current.dateComponents(in: .current, from: after) (lldb) po dateComponents ▿ calendar: gregorian (current) timeZone: Asia/Tokyo (current) era: 1 year: 2019 month: 1 day: 30 hour: 23 minute: 6 second: 8 nanosecond: 585582017 weekday: 4 weekdayOrdinal: 5 quarter: 0 weekOfMonth: 5 weekOfYear: 5 yearForWeekOfYear: 2019 isLeapMonth: false ▿ calendar : gregorian (current) - identifier : Foundation.Calendar.Identifier.gregorian - kind : "current" ▿ locale : Optional<Locale> ▿ some : en_JP (current) - identifier : "en_JP" - kind : "current" ▿ timeZone : Asia/Tokyo (current) - identifier : "Asia/Tokyo" - kind : "current" ▿ abbreviation : Optional<String> - some : "JST" - secondsFromGMT : 32400 - isDaylightSavingTime : false - firstWeekday : 1 - minimumDaysInFirstWeek : 1 ▿ timeZone : Asia/Tokyo (current) - identifier : "Asia/Tokyo" - kind : "current" ▿ abbreviation : Optional<String> - some : "JST" - secondsFromGMT : 32400 - isDaylightSavingTime : false - era : 1 - year : 2019 - month : 1 - day : 30 - hour : 23 - minute : 6 - second : 8 - nanosecond : 585582017 - weekday : 4 - weekdayOrdinal : 5 - quarter : 0 - weekOfMonth : 5 - weekOfYear : 5 - yearForWeekOfYear : 2019 - isLeapMonth : false ------------------------------------------------------------------------------- //通知飛んで来る //2019/1/30 23:06 実行 var dateComponents = DateComponents() dateComponents.year = 2019 dateComponents.month = 1 dateComponents.day = 30 dateComponents.hour = 23 dateComponents.minute = 7 dateComponents.second = 0 (lldb) po dateComponents ▿ year: 2019 month: 1 day: 30 hour: 23 minute: 7 second: 0 isLeapMonth: false - year : 2019 - month : 1 - day : 30 - hour : 23 - minute : 7 - second : 0 - isLeapMonth : false
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/30 14:03
2019/01/31 12:52