重复通知间隔

编程入门 行业动态 更新时间:2024-10-12 12:30:06
本文介绍了重复通知间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有一个选项可以设置UILocalNotification的重复间隔.由于Apple已弃用UILocalNotification并建议改用UNNotification,因此我找不到用UNNotification设置自定义重复间隔通知的方法.

There is an option to set the repeat interval for UILocalNotification. Since Apple has deprecated UILocalNotification and recommend to use UNNotification instead, I couldn't find a way to set a notification with custom repeat interval with UNNotification.

var comp = DateComponents() comp.year = 2019 comp.month = 1 comp.day = 9 comp.hour = 14 comp.minute = 14 comp.second = 0 let calendar = Calendar.current let notification: UILocalNotification = UILocalNotification() notification.category = "Daily Quote" notification.alertBody = "Body" notification.alertTitle = "Title" notification.fireDate = calendar.date(from: comp) notification.repeatInterval = NSCalendar.Unit.day UIApplication.shared.scheduleLocalNotification(notification)

那么我可以设置一个类似的通知,在使用新的UNNotification等待初始通知后每小时或每天重复一次吗?

So can I set a similar notification that repeats hourly or daily after waiting for the initial notification using the new UNNotification?

推荐答案

要模仿UILocalNotification的API fireDate和repeatInterval,您可以创建两个触发器,一个不重复,将用于fireDate启动和repeatInterval的其他重复.

To mimic the UILocalNotification's API fireDate and repeatInterval you can create two triggers, one non-repeating which would be used for fireDate to kickoff and other repeating for repeatInterval.

这是一个例子:

import UserNotifications /// Schedules notificaiton to fire at specific date, and then it repeats by specified repeat component /// (week, day, hour, etc.) and repeat interval. For example to repeat every 20minutes repeatComponent /// would be .minute and repeatInterval would be 20. /// - Parameters: /// - fireDate: Date for initial notification delivery /// - repeatComponent: Component by which repeating would be performed (week, day, hour, etc.) /// - repeatInterval: Interval by which repeating by specified component would be performed. Defaults value is 1. func scheduleNotification(fireDate: Date, repeatComponent: Calendar.Component, repeatInterval: Int = 1) { let content = UNMutableNotificationContent() content.title = "Daily Quote" content.body = "Inspirational quote." content.categoryIdentifier = "quote.category" UNUserNotificationCenter.current().requestAuthorization( options: [.alert,.sound]) { (granted, error) in if let error = error { print("granted, but Error in notification permission:\(error.localizedDescription)") } let fireTrigger = UNTimeIntervalNotificationTrigger(timeInterval: fireDate.timeIntervalSinceNow, repeats: false) let fireDateRequest = UNNotificationRequest(identifier: "quote.starter", content: content, trigger: fireTrigger) UNUserNotificationCenter.current().add(fireDateRequest) {(error) in if let error = error { print("Error adding firing notification: \(error.localizedDescription)") } else { if let firstRepeatingDate = Calendar.current.date(byAdding: repeatComponent, value: repeatInterval, to: fireDate) { let repeatingTrigger = UNTimeIntervalNotificationTrigger(timeInterval: firstRepeatingDate.timeIntervalSinceNow, repeats: true) let repeatingRequest = UNNotificationRequest(identifier: "quote.repeater", content: content, trigger: repeatingTrigger) UNUserNotificationCenter.current().add(repeatingRequest) { (error) in if let error = error { print("Error adding repeating notification: \(error.localizedDescription)") } else { print("Successfully scheduled") // Successfully scheduled } } } } } UNUserNotificationCenter.current().delegate = self } }

委托(用于调试):

extension ViewController: UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print("\(notification.request.identifier): \(Date())") UNUserNotificationCenter.current().getPendingNotificationRequests { (requests) in for request in requests { if let timeIntervalTrigger = request.trigger as? UNTimeIntervalNotificationTrigger { print(Date(timeIntervalSinceNow: timeIntervalTrigger.timeInterval)) } } } } }

满足您的要求的用途:

let interval = 7 // One week from now if let fireDate = Calendar.current.date(byAdding: .day, value: interval, to: Date()) { _ = scheduleNotification(fireDate: fireDate, repeatComponent: .day) }

注意

NOTE

指定小于60秒的重复间隔将导致以下异常:

Specifying repeating interval less than 60sec would result with exception:

"NSInternalInconsistencyException",原因:如果重复,时间间隔必须至少为60"

'NSInternalInconsistencyException', reason: 'time interval must be at least 60 if repeating'

更多推荐

重复通知间隔

本文发布于:2023-11-27 10:52:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   通知

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!