iOS 10 用户通知不允许每隔一段时间重复通知



http://www.openradar.me/26855019

使用新的iOS 10用户通知框架,无法再将通知安排在每分钟或每小时重复一次的特定日期或时间。

如何在用户通知框架中执行此操作?

我找到了这个:

https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/SchedulingandHandlingLocalNotifications.html#//apple_ref/doc/uid/TP40008194-CH5-SW1

您可以尝试为 UNNotificationResponse 定义类别,然后根据类别类型处理响应。

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.notification.request.content.categoryIdentifier == "TIMER_EXPIRED" {
// Handle the actions for the expired timer.
if response.actionIdentifier == "SNOOZE_ACTION" {
// Invalidate the old timer and create a new one. . .
}
else if response.actionIdentifier == "STOP_ACTION" {
// Invalidate the timer. . .
}
}
// Else handle actions for other notification types. . .
}

您可以根据时间、日历或位置触发通知。触发器可以重复:

let date = Date(timeIntervalSinceNow: 60)
let triggerMinute = Calendar.current.dateComponents([.minute,.second], from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerMinute, repeats: true)

此代码尝试计划通知,以便日期的分钟和秒部分匹配,您可以按照所需的方式配置它。

最新更新