UNUserNotification在计划以后的日期时永远不会出现

时间:2017-04-10 13:43:32

标签: ios swift uilocalnotification unnotificationrequest

当我计划将来近一天,例如1小时后,会显示通知,但是当我计划明天或今晚以后,它们从未显示在我的真实数据上。有关为什么会发生这种情况的任何想法?我仔细检查了trigger.nextTriggerDate(),它们是对的

这是我的代码:

@available(iOS 10.0, *)
class func scheduleWith(episode: Episode, dateInfo: DateComponents) {
    let content = UNMutableNotificationContent()
    content.title = episode.title
    content.body = episode.overview

    content.userInfo = ["id": episode.id]
    content.categoryIdentifier = Notify.category.episodes.str

    let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)

    let request = UNNotificationRequest(identifier: Notify.identifier.episode.str, content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
        if let error = error {
            print(error)
        }
        else {
            print("date info", dateInfo.date)
            print("trigger", trigger.nextTriggerDate()!)
        }
    })
}

以下是我如何创建dateInfo:

let greg = Calendar.current
var components = DateComponents()
components.setValue(-15, for: .minute)

guard let date = ep.firstAired as Date? else { return }
guard let alertDate = greg.date(byAdding: components, to: date) else { return }

let dateInfo = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: alertDate)

1 个答案:

答案 0 :(得分:2)

您的通知使用相同的标识符。您需要为循环内的每个通知指定不同的标识符。