iOS - 安排多个本地通知,UNCalenderNotificationTrigger

时间:2017-04-08 07:38:34

标签: ios swift notifications uilocalnotification localnotification

我想开发一个提醒应用,需要在我们设置提醒时发送本地通知。我开发了一些代码,即使我设置了多个通知也只触发一个通知。如果我在晚上7:30,晚上7:31,晚上7:32设置提醒,则通知仅在晚上7:32显示。

这是我的代码

    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: components.hour, minute: components.minute)
    let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: true)
    let content = UNMutableNotificationContent()
    content.title = "Reminder"
    content.body = "To day is the last date for the payment of your card. Please make payment today."
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "PaymentReminderIdentifier"
    let request = UNNotificationRequest(identifier: "ReminderNotification", content: content, trigger: trigger)
    UNUserNotificationCenter.current().delegate = self
    //UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
    UNUserNotificationCenter.current().add(request) {(error) in
        if let error = error {
            print("Uh oh! We had an error: \(error)")
        }
    }
}

//

func notif() {
    let selectedDate = fullDate
    print("Selected date: \(selectedDate)")
    let delegate = UIApplication.shared.delegate as? AppDelegate
    delegate?.scheduleNotification(at: selectedDate!)
}

我想在每次创建提醒时发送通知。 帮我解决这个问题。谢谢......

1 个答案:

答案 0 :(得分:2)

每次使用不同的标识符。它会起作用