安排未来的联合国大学通知中心,并附有条件和限制 (iOS)



我知道这已经被问了好几次,但我在SO上没有找到具体的解决方案。我正在开发iOS应用程序,我正在使用userNotification,所有事情都运行良好,但是我必须使用重复/重复通知,所以这是我必须满足的条件,想知道如何

案例:向用户显示通知,以检查特定任务是否完成。

  1. 每天显示通知,直到指定日期到达
  2. 每周一显示通知,直到指定日期到达
  3. 每月显示通知 每指定日期,直到指定日期到达

问题:

  1. 我不知道如何为给定条件安排重复通知
  2. 每个
  3. 应用程序的通知是否有任何限制?假设我有 100 个任务,并且每个任务可能具有与给定条件绑定的任何通知。

请告诉我可以做什么?以及我如何满足我的要求。

每天显示通知,直到指定日期到达

这是为了每天在特定时间显示一条消息

var dateComponents = DateComponents()
dateComponents.hour = 10
dateComponents.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
if trigger.nextTriggerDate() >= your_specified_date {
return
}

每个应用程序的通知是否有限制? 假设我有 100 个任务 并且每个任务都可以将任何通知与 uppar 绑定 给定条件。

是的,有限制。系统会保留最快触发的 64 个通知(自动重新安排的通知计为单个通知(,并丢弃其余通知。

带有正文的自定义日期的通知计划

func schedulingNotificationForCustomDate(body: String, currentDate: Date) {
// Configure User Notification Center
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = ""
content.subtitle = ""
content.body = body
content.categoryIdentifier = "WeekDay notification"
content.userInfo = ["NotificationID": "WeekDay notification", "repeat": true, "reschedule": false]
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: currentDate.timeIntervalSince1970, repeats: true)
let request = UNNotificationRequest(identifier: "WeekDay notification", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
}

根据要求创建日期

extension Date {
static func createDate(weekday: Int, hour: Int, minute: Int, year: Int) -> Date {
let calendar = Calendar.current
var components = DateComponents()
components.hour = hour
components.minute = minute
components.year = year
components.month = calendar.component(.month, from: Date())
components.day = calendar.component(.day, from: Date())
components.weekday = weekday // sunday = 1 ... saturday = 7
components.timeZone = .current
return calendar.date(from: components)!
}
}

获取安排重复通知的下一个日期

extension Date {
static func nextNthDateFromCurrentDate(FromDate: Date, numberOfDays: Int) -> Date {
return Calendar.current.date(byAdding: .day, value: numberOfDays, to: FromDate)! // where numberOfDays is the number of days for the next date
}
}

用于安排通知呼叫

self.schedulingNotificationForCustomDate(body: body, currentDate: Date.nextNthDateFromCurrentDate(FromDate: Date.createDate(weekday: 2, hour: 11, minute: 00, year: year), numberOfDays: 14))

重复本地通知没有结束日期规范,因此不幸的是,您需要构建一些自定义内容。但是,根据Apple的说法,这是您可以排队的64个通知的限制。

一个应用只能有有限数量的计划通知; 系统保持最快触发的 64 条通知(自动 重新安排的通知计为单个通知(和 丢弃其余的。

我会有一个扩展程序,可以抓取两个日期之间的日期,如下所示:

extension Date {
static func dates(from fromDate: Date, to toDate: Date) -> [Date] {
var dates: [Date] = []
var date = fromDate
while date <= toDate {
dates.append(date)
guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
date = newDate
}
return dates
}
}

然后,您可以获取所需的日期并循环访问每个日期以创建通知:

let threeDaysFromNowExample = Calendar.current.date(byAdding: .day, value: 3, to: Date())!
let dates = Date.dates(from: Date(), to: threeDaysFromNowExample)
for date in dates {
let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Your alert here!"
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
}

没有测试此代码,但应该可以让你开始。例如,您可能需要调整扩展程序以添加仅抓取星期一的功能。

最新更新