I’m creating an iOS app and I am dealing with some difficulties. The issue I am dealing with is as follows: I wish to implement a performance utilizing UNUserNotification to register a bunch of notifications with particular instances. As soon as the desired time is reached, the group of notifications needs to be triggered and proceed to be despatched in an countless loop till the person manually stops them.
I’ve two approaches to attain this:
- Utilizing UNTimeIntervalNotificationTrigger
func sendNotification(){
for i in 0...20{
let content material = UNMutableNotificationContent()
content material.title = "HIIT Timer(i)"
content material.physique = "TimeInterval physique"
content material.sound = UNNotificationSound.init(named: UNNotificationSoundName(rawValue: "elephantt.caf"))
content material.categoryIdentifier = "timer.class"
let set off = UNTimeIntervalNotificationTrigger(
timeInterval: TimeInterval(60 + (3 * i)), repeats: true)
let request = UNNotificationRequest(identifier: "timer.request(i)", content material: content material, set off: set off)
UNUserNotificationCenter.present().add(request) { (error) in
if let error = error{
print("Error posting notification:(error.localizedDescription)")
}else {
print("Notification scheduled efficiently")
}
}
}
On this code, I used UNTimeIntervalNotificationTrigger and registered 20 notifications. Every notification has a time interval of three seconds and repeats set to true. After one minute, the notifications are triggered each three seconds in an infinite loop. Nonetheless, the issue lies with the time interval parameter. Though I can set it to 1000 to set off the notification after 1000 seconds, the following set off may even happen 1000 seconds later. It isn’t possible for me to register 333 notifications to attain a steady loop. So, if I wish to use UNTimeIntervalNotificationTrigger to attain this impact, I need to set the preliminary set off time and repeat interval individually. I am undecided if there may be one other method to obtain this.
- Utilizing UNCalendarNotificationTrigger
func cnTestFromSOF(){
var referenceDate = Calendar.present.date(byAdding: .second, worth: 5, to: Date())!
for i in 0...20 {
let content material = UNMutableNotificationContent()
content material.title = "Notification (i)"
content material.physique = "Physique"
content material.sound = .default
var dateComponents = DateComponents(calendar: Calendar.present)
dateComponents.second = 3
//dateComponents.minute = X
guard let nextTriggerDate = dateComponents.calendar?.date(byAdding: dateComponents, to: referenceDate),
let nextTriggerDateCompnents = dateComponents.calendar?.dateComponents([.second], from: nextTriggerDate) else {
return
}
referenceDate = nextTriggerDate
let set off = UNCalendarNotificationTrigger(dateMatching: nextTriggerDateCompnents, repeats: true)
let request = UNNotificationRequest(identifier: "notif-(i)", content material: content material, set off: set off)
UNUserNotificationCenter.present().add(request)
}
}
On this code, I used UNCalendarNotificationTrigger and likewise registered 20 notifications. Every notification has a time distinction of three seconds, and the repeats parameter is about to true. On this methodology, I set the dateMatching’s dateComponents to solely embody [.second], so the notification is triggered each 3n seconds. The issue right here is that the notification is triggered each minute after registration. Nonetheless, if I set it to [.minute, .second], it means the notification will solely be triggered on the 3n seconds of the xth minute of every hour. Since every hour has just one xth minute, the notification will not repeat till the following xth minute of the next hour. I may set it to [.day, .hour, .second] to specify that the notification needs to be triggered each 3n seconds in a particular hour of a particular day. However this implies the notification can solely begin triggering on the hour, and I can not obtain triggering it at a particular hour and minute.
- I’ve thought-about one other strategy, which is to set it within the UNUserNotificationCenterDelegate strategies. First, register a notification with UNTimeIntervalNotificationTrigger or UNCalendarNotificationTrigger to set off a notification at a particular time. Then, when the notification is triggered, deal with it within the delegate methodology and register 20 repetitive notifications utilizing strategy 1 or 2 to attain the infinite loop of notifications. Nonetheless, I discovered that this delegate would not have callbacks for when the person receives the notification; it solely has callbacks for when the person clicks on the notification or when the app is within the foreground. This strategy would not appear appropriate for attaining the specified impact.
I’ve seen alarm clock apps that implement this performance, the place the app continues to set off notifications endlessly and steadily as soon as the alarm time is reached. So, it’s attainable to attain this impact. I simply cannot discover related info or implementation strategies. I’ve looked for this difficulty on Stack Overflow as nicely and located some comparable questions, however none of them offered a transparent reply. I hope somebody with expertise or concepts may also help me reply this query. Thanks prematurely on your response.”