swift – 用户通知3天后重复每天/小时 – iOS 10
发布时间:2020-12-14 05:44:20 所属栏目:百科 来源:网络整理
导读:UILocalNotification已被折旧,因此我想将我的代码更新为UserNotification框架: let alertDays = 3.0let alertSeconds = alertDays * 24.0 * 60.0 * 60.0let localNotification:UILocalNotification = UILocalNotification()localNotification.alertAction =
UILocalNotification已被折旧,因此我想将我的代码更新为UserNotification框架:
let alertDays = 3.0 let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 let localNotification:UILocalNotification = UILocalNotification() localNotification.alertAction = "Reminder" localNotification.alertTitle = "Reminder Title" localNotification.alertBody = "Reminder Message" localNotification.fireDate = Foundation.Date(timeIntervalSinceNow: alertSeconds) localNotification.repeatInterval = .day UIApplication.shared().scheduleLocalNotification(localNotification) 在等待初始通知后,如何使用UserNotification框架设置类似的每日或每小时重复? let alertDays = 3.0 let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 let content: UNMutableNotificationContent = UNMutableNotificationContent() content.title = "Reminder Title" content.subtitle = "Reminder Subtitle" content.body = "Reminder Message" let calendar = Calendar.current let alarmTime = Foundation.Date(timeIntervalSinceNow: alertSeconds) let alarmTimeComponents = calendar.components([.day,.hour,.minute],from: alarmTime) let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents,repeats: true) let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,content: content,trigger: trigger) UNUserNotificationCenter.current().add(request) { (error) in // ... }
看起来这似乎不受支持,但为了制定解决方法,您可以使用:
let alertDays = 3.0 let daySeconds = 86400 let alertSeconds = alertDays * daySeconds let content: UNMutableNotificationContent = UNMutableNotificationContent() content.title = "Reminder Title" content.subtitle = "Reminder Subtitle" content.body = "Reminder Message" let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds),repeats: false) let request = UNNotificationRequest(identifier: workoutAlarmIdentifier,trigger: trigger) UNUserNotificationCenter.current().add(request) { (error) in // ... } 与didReceive(_:withContentHandler:)结合使用,您可以使用: let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds),repeats: false) 我知道这不是最佳的,但它应该不使用已弃用的类/方法.您使用重复:false,因为您在用户收到通知并创建新通知之前拦截通知.此外,如果您处理多个通知,则可以将它与UNNotificationAction和UNNotificationCategory结合使用. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |