Scheduled notification for launch at a specific time - Swift

In my application, I have a uidatepickerview that allows the user to select a time. Currently, in my code, I have chosen this time as dateA. Then I went over all the days selected in a separate uitableview and checked if they were equal to today (for example, Tuesday). If the days were the same, then I sent a notification at the selected time, which was transmitted as dateA. However, when I try to send a notification, it is not sent.

Here is a snippet of my code showing what I tried:

    var dateA: Date? = nil//where selected time is kept

    var weekdaysChecked = [String]()//where selected weekdays are kept

    var alarms = [Alarm]() {
        didSet {
            tableView.reloadData()
        }
    }


    override func viewDidLoad() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound], completionHandler: { (didAllow, error) in
        })

    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "alarmCell", for: indexPath) as! DisplayAlarmCell

        let row = indexPath.row
        let alarm = alarms[row]
        let date = Date()
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat  = "EEEE"//"EE" to get short style
        let dayInWeek = dateFormatter.string(from: date)


        if(alarms.count == 40) {
            self.navigationItem.rightBarButtonItem?.isEnabled = false
            tableView.reloadData()
        }

        cell.alarmTitle.text = alarm.alarmLabel
        cell.clockTitle.text = alarm.time

        for weekdays in weekdaysChecked {
            if(dayInWeek == weekdays){
                let content = UNMutableNotificationContent()
                content.title = alarm.alarmLabel!
                content.subtitle = alarm.time!
                content.sound = UNNotificationSound(named: "Spaceship_Alarm.mp3")

                let trigger = Calendar.current.dateComponents([.hour,.minute], from: dateA!)
                let triggerNotif = UNCalendarNotificationTrigger(dateMatching: trigger, repeats: false)

                let triggerRequest = UNNotificationRequest(identifier: "AlarmNotif", content: content, trigger: triggerNotif)

                UNUserNotificationCenter.current().add(triggerRequest, withCompletionHandler: nil)

                print("This is the correct day.")
            }
        }
+4
source share
2 answers

, " ", UNNotificationRequest. : " , ".

, , requestAuthorization . , - , .


, cellForRowAt . , . , , . , ( ) , .

, ( , ..).

+1

:

        let trigger = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute], from: dateA!)

AppDelegate

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

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    completionHandler()
}

, - .

,

0

Source: https://habr.com/ru/post/1681735/


All Articles