ios - 如何快速安排每日推送通知?

标签 ios objective-c swift ios7 ios8

我希望我的应用每天下午 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/

相关文章:

ios - 为什么在 Objective C 中总是说属性是非原子的?

ios - UITableView 可见单元自动布局后计算 View 位置

ios - 我应该使用什么尺寸将背景图像放在导航栏上

ios - 为什么我的 NSMutableURLRequest 失败?

ios - UISplitViewController从master返回到detail

ios - 在 CollectionView 中滚动 tableView 时隐藏 NavigationBar?

iOS 获取哪个 uielement 调用了 UIKeyboardWillShowNotification

ios - ScrollView contentSize 大于边界,但没有滚动

ios - 在 UITableView 中禁用部分 Cell

objective-c - 需要一种更高效的方法来将大量 float 存储到数组中并将它们读回