【问题标题】:iOS How to create schedule local notification by days, weeks, and hourly SwiftiOS 如何按天、周和每小时 Swift 创建计划本地通知
【发布时间】:2018-08-13 06:29:15
【问题描述】:

我想为用户创建一个时间表,目前,我知道如何创建一个特定的时间,但我想创建这样的通知,例如针对不同的日子,或者只针对周末,或者针对哪个用户设置的日期. 我的每小时通知代码:

var notifTime = Int()
var notifMin = Int()

func test() {
    let content = UNMutableNotificationContent()
      content.title = "Test"
        content.body = "It's test bro"
        content.sound = UNNotificationSound.default()

        let gregorian = Calendar(identifier: .gregorian)
        let now = Date()
        var components = gregorian.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)

        components.hour = notifTime
        components.minute = notifMin
        components.second = 0

        let date = gregorian.date(from: components)!

        let triggerDaily = Calendar.current.dateComponents([.hour,.minute,.second,], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDaily, repeats: true)

        let request = UNNotificationRequest(identifier: "any", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

对于时间选择,我使用日期选择器。

那么我怎样才能创建每天的日程安排通知呢?

【问题讨论】:

  • 我认为您没有为 notifTime 和 notifMin 分配任何值,因此您遇到了问题。那么,能否说明一下 notifTime 和 notifMin 背后的逻辑?,那么,很容易找到解决方案。
  • @Pratik Sodha 为此变量 notifTime 和 notifMin 我从日期选择器获取数据

标签: ios swift notifications


【解决方案1】:
let startDate = Date()
let triggerDate = Calendar.current.dateComponents([.weekday,.hour,.minute], from: startDate)

startDate设置为第一个通知时间日期。 希望它对你有用。

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 1970-01-01
    • 2014-08-27
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    相关资源
    最近更新 更多