当我安排将来的某个日期(例如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)

最佳答案

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

关于ios - UNUserNotification永远不会在以后安排的日期出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43324900/

10-11 00:29