由于UILocalNotification在iOS10中被废弃,我很难理解如何将下面的代码更新到UNNotificationRequest框架中。我基本上是让用户在他们选择的时间安排一天的通知。例如,如果他们想每天上午11:00收到通知下面的代码适用于iOS10以下的UILocalNotification版本,但由于不推荐使用UILocalNotification,因此不再工作。任何帮助都是非常感谢的。
let notification = UILocalNotification()
notification.fireDate = fixedNotificationDate(datePicker.date)
notification.alertBody = "Your daily alert is ready for you!"
notification.timeZone = TimeZone.current
notification.repeatInterval = NSCalendar.Unit.day
notification.applicationIconBadgeNumber = 1
UIApplication.shared.scheduleLocalNotification(notification)发布于 2020-10-06 06:29:49
您可以使用UNCalendarNotificationTrigger创建一个使用UNUserNotificationCenter反复触发的通知。你可以做这样的事。诀窍是只在触发日期中包含时间组件。
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Attention!"
content.body = "Your daily alert is ready for you!"
content.sound = UNNotificationSound.default
let identifier = "com.yourdomain.notificationIdentifier"
var triggerDate = DateComponents()
triggerDate.hour = 18
triggerDate.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDate, repeats: true)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request, withCompletionHandler: { (error) in
if let error = error {
// Something went wrong
print("Error : \(error.localizedDescription)")
} else {
// Something went right
print("Success")
}
})发布于 2020-10-05 16:46:32
不能安排每天重复的通知。这个通知只会发生一次,然后你必须重新安排它,这意味着你必须再次打开应用程序。
在BGTask 13中引入了iOS API,它可以用来执行一些后台任务,但不是这个任务,不能将任务调度到特定的时间,.This API上一次只能在应用程序处于后台时才能工作,而不是当它被杀死时。您只能设置一些时间间隔,系统将使用它作为指导点,以确定何时执行应用程序的代码。但以我的经验来看,这是相当不可靠的。
实现这一点的唯一方法是实现远程推送通知。即使如此,推送通知也能在应用程序被杀死时起作用。
https://stackoverflow.com/questions/64212375
复制相似问题