user2982195
user2982195

Reputation: 153

Swift Notifications multiple Banners

I know this topic is familiar, but I have a huge problem with the notification banners. I already studied the most stackoverflow threads about this topic, but there weren't the correct answers.

My problem: I want to repeat a Notification every day at 10 pm. It repeats correctly, on the first and second day. On the third and fourth day it repeated 3 times at 10pm with the same banner.

This is my code:

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)
        }
    }

Very straight forward, very easy, you might think ...

What I already tried: - Installing the application completely new - Installing it with a new bundleId - Updated my iOS because of the previous bug in iOS 9.X

I hope someone can help me.

Upvotes: 0

Views: 366

Answers (1)

battlmonstr
battlmonstr

Reputation: 6300

Try calling removeAllPendingNotificationRequests before adding one.

Every time you call "add" it adds a new one (even though the title/body/time is the same).

Upvotes: 1

Related Questions