困っていること
timeintervalだと正常に動く(通知が鳴る)がdateMatchingだと動かない。
#該当コード
swift
1 2 3import UIKit 4import UserNotifications 5 6class ViewController: UIViewController { 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 } 11 12 override func didReceiveMemoryWarning() { 13 super.didReceiveMemoryWarning() 14 } 15 16 @IBAction func notifyTapped(_ sender: Any) { 17 let content = UNMutableNotificationContent() 18 content.title = "今から一分後に日時指定" 19 content.body = "日時指定" 20 content.sound = UNNotificationSound.default() 21 let 一分後 = DateComponents(minute:60) 22 var 通知を鳴らす時間date = Calendar.current.date(byAdding: 一分後, to: Date()) 23 var 通知を鳴らす時間datecompo = Calendar.current.dateComponents([.year, .month, .day,.hour,.minute], from: 通知を鳴らす時間date!) 24 let trigger = UNCalendarNotificationTrigger(dateMatching: 通知を鳴らす時間datecompo, repeats: false) 25 let request = UNNotificationRequest(identifier: "immediately", content: content, trigger: trigger) 26 print(request) 27 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 28 } 29 30 31 @IBAction func nofityLaterTapped(_ sender: Any) { 32 let content = UNMutableNotificationContent() 33 content.title = "3秒後" 34 content.body = "タイムインターバル" 35 content.sound = UNNotificationSound.default() 36 37 38 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) 39 let request = UNNotificationRequest(identifier: "later", content: content, trigger: trigger) 40 print(request) 41 UNUserNotificationCenter.current().add(request, withCompletionHandler: nil) 42 } 43 44} 45 46
#動かないリクエストのコンソール出力
<UNNotificationRequest: 0x600002425980; identifier: immediately, content: <UNNotificationContent: 0x6000011b88f0; title: <redacted>, subtitle: (null), body: <redacted>, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
), badge: (null), sound: <UNNotificationSound: 0x600000198380>, realert: 0, trigger: <UNCalendarNotificationTrigger: 0x600002a24920; dateComponents: <NSDateComponents: 0x6000028b2c50> {
Calendar Year: 2020
Month: 5
Leap Month: 0
Day: 17
Hour: 14
Minute: 58, repeats: NO>>
最後に
拙い質問で恐縮ですがヒントだけでも教えて頂ければ嬉しいです。
あなたの回答
tips
プレビュー