I am writing a simple timer app in SwiftUI, but I can't figure out how best to send completion push notifications. I have a countTo variable, which is the number of seconds scheduled, and a counter variable that keeps track of the current second.
Considering the user can pause, play and end the timer, what would be the best way to send a notification once countUp == counter? I can't schedule a set time because that doesn't account for pausing and playing. Would it involve cancelling the notification and then rescheduling it when the user resumes the timer?
Thanks for any help, much appreciated!
CodePudding user response:
Probably you're talking about local notifications. Push notifications are sent via APNS and the app can't send them.
For local notifications, look into UNNotificationRequest
and different triggers (UNNotificationTrigger
).
It looks like the UNTimeIntervalNotificationTrigger
will work best for your task. When the user pauses the playback or the playback gets interrupted by the system, you'll have to cancel an existing notification and then schedule another one once the playback is resumed.
CodePudding user response:
I think you can refer to this documentation.