0

iOS13.4、XCode11.4、Swift5.2、

平日 (つまり、月曜日から金曜日で、土曜日と日曜日ではない) にトリガーされるローカル通知をどのように設定できますか??

別の Calendar-Component セットアップを試みましたが、workday-problem には成功しませんでした。

これが私がこれまでに行ったことです...

アラームを特定の日付にする必要がある場合:

Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)

アラームを毎日繰り返す必要がある場合:

Calendar.current.dateComponents([.hour, .minute], from: notificationDate)

特定の曜日にアラームを繰り返す必要がある場合:

Calendar.current.dateComponents([.weekday, .hour, .minute], from: notificationDate)

しかし、複数の平日 (月曜日から金曜日までのすべての平日など) にどのように設定しますか??

正確な notificationDate の例のコードは次のとおりです。

import UIKit

class MyViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        let content = UNMutableNotificationContent()
        content.title = "My Notification Title"
        content.categoryIdentifier = UUID().uuidString
        var notificationIdentifier = "23224-23134-234234"
        var notificationDate = Date(timeInterval: 30, since: Date())
        let notificationTriggerKind = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)
        let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: notificationTriggerKind, repeats: false)
        let notificationRequest = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: notificationTrigger)
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.delegate = self
        notificationCenter.add(notificationRequest) {(error) in if let error = error { print("error: \(error)") } }
    }
}

extension MyViewController: UNUserNotificationCenterDelegate {

    // kicks in when App is running in Foreground (without user interaction)
    func userNotificationCenter(_ center: UNUserNotificationCenter,
            willPresent notification: UNNotification,
            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        // ...do your custom code...
        completionHandler([.alert, .sound])
    }

    // kicks in when App is running in Foreground or Background
    // (AND App is open) AND user interacts with notification
    func userNotificationCenter(_ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse, withCompletionHandler
        completionHandler: @escaping () -> Void) {

        // ... do your custom code ....
        return completionHandler()
    }
}
4

1 に答える 1