我希望我的应用每天下午 13 点通知用户。我将其放入我的应用程序委托(delegate)中:
func Notification() {
var Notification = UILocalNotification()
Notification.alertAction = "New Question!"
Notification.alertBody = "Solve the new question!"
Notification.fireDate = NSDate(timeIntervalSinceNow: 0)
UIApplication.sharedApplication().scheduleLocalNotification(Notification)
}
这在我的应用程序中 didFinishLaunching:
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
我需要做什么才能安排每天触发我的通知?
最佳答案
只需设置任何过去的日期,即下午 13 点,
并将重复间隔设置为 .CalendarUnitDay
let startDate = Date()
var component = Calendar.current.dateComponents([.year, .month, .day, .hour], from: Date())
component.hour = 13
let fireDate = Calendar.current.date(from: component)
notification.repeatInterval = .day;
notification.fireDate = fireDate
更新:
let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: true)
var triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second], from: Date())
triggerDaily.hour = 13
triggerDaily.minute = 0
triggerDaily.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)
let content = UNMutableNotificationContent()
content.title = "title"
content.body = "body"
content.categoryIdentifier = "todoList"
let request = UNNotificationRequest(identifier: "identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in // to do }
关于ios - 如何快速安排每日推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665433/