Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Swift 3为日期数组安排通知_Ios_Swift_Unnotificationtrigger - Fatal编程技术网

Ios 使用Swift 3为日期数组安排通知

Ios 使用Swift 3为日期数组安排通知,ios,swift,unnotificationtrigger,Ios,Swift,Unnotificationtrigger,我有一个包含日期的数组。我想把这几天的通知安排在早上6:30 我遵循了,这有助于根据datepicker的输入安排通知,这很好,但我有点不确定如何调用我的函数将通知安排在给定的日期 那么我的问题是如何以及在何处调用该函数? 这些天是连续的 我可以给函数一个开始日期,并用数组中的项数重复它吗 以下是我的职责: func scheduleNotification(at date: Date) { let calendar = Calendar(identifier: .g

我有一个包含日期的数组。我想把这几天的通知安排在早上6:30

我遵循了,这有助于根据datepicker的输入安排通知,这很好,但我有点不确定如何调用我的函数将通知安排在给定的日期

那么我的问题是如何以及在何处调用该函数?

  • 这些天是连续的
  • 我可以给函数一个开始日期,并用数组中的项数重复它吗
以下是我的职责:

    func scheduleNotification(at date: Date) {
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents(in: .current, from: date)
        let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: 6, minute: 30)
        let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)

        let content = UNMutableNotificationContent()
        content.title = "Advent Calendar"
        content.body = "Just a reminder to open your present!"
        content.sound = UNNotificationSound.default()

        let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
 UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().add(request) {(error) in
            if let error = error {
                print("Uh oh! We had an error: \(error)")
            }
        }
    }

是的,在咨询了一位开发人员之后,我决定使用一个简单的for循环(重复24次),它将索引传递到函数的day属性,在那里我预先配置了小时、分钟、年和月,如下所示:

func scheduleNotification(day: Int) {

    var date = DateComponents()
    date.year = 2016
    date.month = 11
    date.day = day
    date.hour = 6
    date.minute = 30

    let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
}
和for循环:

for index in 1...24 {
        scheduleNotification(day: index)
    }
因为我在AppDelegate中设置了所有内容,所以我调用了
didfishlaunchingwithoptions中的函数

12月1日更新。


所以我把一切都保持原样,但早上没有收到任何通知。这个新的func scheduleNotification如何与您问题中创建通知的前一个函数交互?所有人的警报内容是否相同alerts@tylerSF,它一次创建所有24个通知,计划每天发送。该应用程序每天都包含一份新礼物,因此通知是相同的,只是为了让用户注意到信息。