UNUserNotification 永远不会在安排在以后的日期出现
UNUserNotification never appear when scheduled for later date
当我安排一个近期的日期(比如 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()!)
}
})
}
下面是我创建日期信息的方式:
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 小时后)时会显示通知,但当我安排明天或今晚晚些时候时,它们不会显示在我的真实数据上。关于为什么会发生这种情况的任何想法?我仔细检查了 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()!)
}
})
}
下面是我创建日期信息的方式:
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)
您正在为您的通知使用相同的标识符。您需要为循环内的每个通知指定不同的标识符。