【问题标题】:How to schedule daily push notifications in swift?如何快速安排每日推送通知?
【发布时间】:2015-10-18 08:48:36
【问题描述】:

我希望我的应用在每天下午 13 点通知用户。我把它放在我的应用委托中:

    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))

我必须做些什么才能让我的通知按计划每天触发?

【问题讨论】:

    标签: ios objective-c swift ios7 ios8


    【解决方案1】:

    只需将任何过去的日期设置为下午 13 点, 并将repeatInterval设置为.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 }
    

    【讨论】:

    • 对不起,不明白你的程序。
    • 你能给我解释清楚吗?谢谢
    • 请注意,UILocalNotification 已被贬值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多