Scheduling local notifications to repeat daily from tomorrow in Swift
Asked Answered
R

1

14

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))")
        }
    }

}
Rania answered 2/7, 2017 at 15:30 Comment(11)
https://mcmap.net/q/902678/-trigger-notification-weekly-swift-3 you can use this weekly as start point you just need to remove the weekday date component to repeat it dailyStammer
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!Rania
Don't forget to add a unique identifier.Stammer
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: pastebin.com/b6rDvczZRania
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://mcmap.net/q/422757/-how-can-i-skip-the-first-occurrence-of-a-repeating-uncalendarnotificationtrigger/2303865Stammer
That does seem to be the case! Thanks for your help anywayRania
Did you find a solution for this?Brentbrenton
Check out this answerEvie
have you found a solution yet?Licence
@DominicWilliams Have you got the solution? Pls let me know. I stuck like you.Golgotha
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 scheduleRania
A
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") 
         }
     }
    
Animality answered 21/6, 2021 at 6:53 Comment(1)
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.Remove

© 2022 - 2024 — McMap. All rights reserved.