13

I'm trying to schedule a local notification to fire every day (i.e. repeats), at a specific time, but from tomorrow.

i.e "Trigger a notifcation every day at 8pm, from tomorrow"

I've been using this SO question as guidance, and I believe I am doing what it says but I am still getting a notification today when I run the following code (if I schedule the notification before 8pm for instance):

    func testDateNotification(){

    let content = UNMutableNotificationContent()
    content.title = "Test"
    content.body = "This is a test"
    let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())

    let userCalendar = Calendar.current
    var components = userCalendar.dateComponents([.hour, .minute], from: tomorrow!)

    components.hour = 20
    components.minute = 00


    let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true)
    let request = UNNotificationRequest(identifier: "test", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request) { (error) in
        if ((error) != nil){
            print("Error \(String(describing: error))")
        }
    }

}
Dominic Williams
  • 1,538
  • 2
  • 13
  • 17
  • https://stackoverflow.com/a/42892780/2303865 you can use this weekly as start point you just need to remove the weekday date component to repeat it daily – Leo Dabus Jul 02 '17 at 16:19
  • I believe that's what I am doing when I only set components.hour and components.minute in my code? Although the nextTriggerDate function is useful as it means I can test without constantly changing my system date! – Dominic Williams Jul 02 '17 at 16:53
  • Don't forget to add a unique identifier. – Leo Dabus Jul 02 '17 at 16:55
  • I have "test" as a identifier in my code? Or am I missing something? If you look at this quick playground, the next trigger date will always be the end of today, rather than the end of tomorrow: https://pastebin.com/b6rDvczZ – Dominic Williams Jul 02 '17 at 16:58
  • You want your notification to repeat daily but you want to skip the first occurrence. I don't think thats possible. there is a similar question https://stackoverflow.com/q/41449749/2303865 – Leo Dabus Jul 02 '17 at 17:27
  • That does seem to be the case! Thanks for your help anyway – Dominic Williams Jul 02 '17 at 18:03
  • Did you find a solution for this? – DS. Apr 24 '18 at 19:48
  • 1
    Check out this [answer](https://stackoverflow.com/questions/54076050/repeat-interval-for-unnotification/54076269#54076269) – Brooketa Jan 16 '19 at 13:26
  • have you found a solution yet? – Joshua Segal Jun 25 '20 at 20:19
  • @DominicWilliams Have you got the solution? Pls let me know. I stuck like you. – Ashu Jul 23 '22 at 07:08
  • My solution was to manually schedule individual notifications for each day in the future for the time I wanted, but you have to be careful as there is a limit on how many notifications you can schedule – Dominic Williams Jul 03 '23 at 08:43

1 Answers1

0

import UserNotifications

  1. Check for user permission

    UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .alert, .sound]) {
    if $0 { } }
    
  2. Add notification

     let fromDate = Date(timeIntervalSince1970: Double(0.0))
    
     let dateComponent = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: fromDate)
    
     let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)
     print(trigger.nextTriggerDate() ?? "nil")
    
     let content = UNMutableNotificationContent()
     content.title = "title"
     content.body = "body"
     let request = UNNotificationRequest(identifier: "identify", content: content, trigger: trigger)
     UNUserNotificationCenter.current().add(request) { 
         if let error = $0 {
             print(error)
             return
         }else { 
             print("scheduled") 
         }
     }
    
Alirza Eram
  • 134
  • 11
  • I have tried with the above code and it is not working as expected as we need to schedule notification from tomorrow. every time "dateComponent" is remains the same. we need to change the component's value for the specific date or +1 .day and if we do above code than the it will repeat on a specific date not date and time. – Azharhussain Shaikh May 18 '22 at 11:52