我知道这个话题很熟悉,但我对通知横幅有很大的疑问。我已经研究了关于这个主题的大多数stackoverflow线程,但没有正确答案。
我的问题:我想每天晚上10点重复通知。它在第一天和第二天正确重复。在第三天和第四天,它在晚上10点用相同的横幅重复了3次。
这是我的代码:
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound])
{
(granted, error) in
if let error = error {
print("granted, but Error in notification permission:\(error.localizedDescription)")
}
}
let notificationContent = UNMutableNotificationContent()
notificationContent.title = "Diary of Health"
notificationContent.body = "Wie war dein Tag?"
var date = DateComponents()
date.hour = 22
date.minute = 00
date.second = 00
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let notificationRequest = UNNotificationRequest(identifier: "\(NSDate().timeIntervalSince1970)", content: notificationContent, trigger: notificationTrigger)
UNUserNotificationCenter.current().add(notificationRequest) { (error) in
if let error = error
{
let errorString = String(format: NSLocalizedString("Unable to Add Notification Request %@, %@", comment: ""), error as CVarArg, error.localizedDescription)
print(errorString)
}
}
非常直接,很容易,你可能会想......
我已经尝试过: - 全新安装应用程序 - 使用新的bundleId安装它 - 由于iOS 9.X中的先前错误而更新了我的iOS
我希望有人可以帮助我。
答案 0 :(得分:1)
在添加之前尝试拨打removeAllPendingNotificationRequests。
每次打电话"添加"它添加了一个新的(即使标题/正文/时间相同)。