Ios Swift通知多个横幅

Ios Swift通知多个横幅,ios,swift,notifications,Ios,Swift,Notifications,我知道这个话题很熟悉,但我对通知横幅有一个很大的问题。我已经研究了关于这个主题的大多数stackoverflow线程,但是没有正确的答案 我的问题是:我想每天晚上10点重复一次通知。它在第一天和第二天正确地重复。在第三天和第四天,它在晚上10点用同样的横幅重复了3次 这是我的代码: UNUserNotificationCenter.current().requestAuthorization( options: [.alert,.sound]) { (gr

我知道这个话题很熟悉,但我对通知横幅有一个很大的问题。我已经研究了关于这个主题的大多数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,因为iOS 9.X上一个bug

我希望有人能帮我。

在添加一个之前,试着打电话


每次您调用“add”时,它都会添加一个新的通知(即使标题/正文/时间相同)。

我认为您的问题在于安排通知。也许你多次调用过这个代码。你在哪里调用了这个代码?我也不明白你的意思。@Mannopson是的,我也想到了这个。也许我必须在我的“firstLaunch”函数中包含代码。嘿,谢谢你的快速回复。我会尝试一下,尽快给你反馈。我应该在哪里叫“removeAllPendingNotificationRequests”?“Update”:很好。第一天就成功了。我期待着下一次。