Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 views
in Technique[技术] by (71.8m points)

swift - iOS local notification base on day, time frame and number of times?

After many researches on Stackoverflow and other websites, the only options I can find are either repeat notification on specific day of the week and specific time using:

dateComponents = DateComponents()  
dateComponents.calendar = Calendar.current  
dateComponents.hour = hour  
dateComponents.minute = minute  
dateComponents.weekday = 1  // Sunday  


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

// the above will trigger every Sunday at whatever time I set it to be.

Another option to send notification is to set time interval but not much options.

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: true)

I cannot find any suggestion or solution to trigger notification (number of times starting at certain time on particular day). For example: I want to trigger 5 local notification every Sunday between 8am to 5pm.

Any suggestion? Thank you in advance.

question from:https://stackoverflow.com/questions/65849056/ios-local-notification-base-on-day-time-frame-and-number-of-times

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to schedule multiple notifications, you need to create multiple notification triggers and register them.

something like:

// Simple extension if you have to create multiple of the same object
extension DateComponents {
   static func triggerFor(hour: Int, minute: Int) -> DateComponents {
      var component = DateComponents()
      component.calendar = Calendar.current
      component.hour = hour
      component.minute = minute
      component.weekday = 1
      return component
   }
}

// Add each trigger into an array with the correct date component
let triggers = [
    UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 1, minute: 0), repeats: true),
    UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 2, minute: 0), repeats: true),
    UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 3, minute: 0), repeats: true),
    UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 4, minute: 0), repeats: true),
    UNCalendarNotificationTrigger(dateMatching: DateComponents.triggerFor(hour: 5, minute: 0), repeats: true)
]

// Iterate through the array and register the notification with the system
for trigger in triggers {
    // Create the content of the notification
    let content = UNMutableNotificationContent()
    content.title = "My Notification Title"
    content.body = "Body of Notification"

    // Create the request
    let uuidString = UUID().uuidString
    let request = UNNotificationRequest(identifier: uuidString, 
                content: content, trigger: trigger)

    // Schedule the request with the system.
    let notificationCenter = UNUserNotificationCenter.current()
    notificationCenter.add(request) { (error) in
       if error != nil {
          // Handle any errors.
       }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...