UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Will this notification fire each day ?

Forums > SwiftUI

I modified the wonderful explanation by Paul Hudson about notifications, adding a DatePicker with just hour and minute, now the notification works perfectly fine for today, but will this fire each day, please suggest

     @State private var wakeUp = Date()

DatePicker("Please set a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
                            .padding()
                            .background(.thinMaterial)
                            .cornerRadius(10)

                        Button("Schedule Notification") {
                            //--

                            let content = UNMutableNotificationContent()
                            content.title = "Write my views"
                            content.subtitle = "Positive work done"
                            content.sound = UNNotificationSound.default

                            // show this notification five seconds from now
                         //   let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
                            let comps = Calendar.current.dateComponents([.hour, .minute], from: wakeUp)

                            let trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: true)

                            print(comps)
                            // choose a random identifier
                            let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

                            // add our notification request
                            UNUserNotificationCenter.current().add(request)

                        }

1      

You set the let trigger = UNCalendarNotificationTrigger(dateMatching: comps, repeats: true) repeats to true so it will set a notification Immediately after the other fire. If you change the time it add a new one but else you cancel the request it will have the first one

Try adding this after UNUserNotificationCenter.current().add(request)

let pendingRequest = UNUserNotificationCenter.current().pendingNotificationRequests()
print("Pending: \(pendingRequest.count)")

1      

If you change the time it add a new one but else you cancel the request it will have the first one

thanks, so does this mean that if i add more notifications, they all will begin to fire, instead of just one per day ?

1      

Yes think it will. You might want to add this

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()

1      

If you want them to set a notification each day then put before it first so when the user taps the button to Schedule Notification then it clears all the pending notification.

If you want it only fire once then change the tigger repeats to false this will then only fire once and not every day

if you want a in depth try these Videos from Stewart Lynch

Swift Local Notifications 1: Authorization

Swift Local Notifications 2: Time Interval Notications

Swift Local Notifications 3: Calendar Notications

Swift Local Notifications 4: Responding to Noticatons

4      

thanks , this will be very helpful

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.