osebas15
osebas15

Reputation: 61

How to show multiple local notifications

Background:

Im writing an application where a bot sends you messages. These messages can be received as local notification.

The Problem:

When the bot sends multiple notifications within a short span of time (1 second between each message), the notification center will only show one message. I will hear the notification sound every time I expect to, But i will still only see the first message.

Relevant code:

func postUserNotification(content: String, delay: TimeInterval, withDictionary dictionary: [String:String] = [:]) {

    let notificationContent = UNMutableNotificationContent()
    notificationContent.body = content
    notificationContent.userInfo = dictionary
    notificationContent.categoryIdentifier = "message"

    let dateAfterDelay = Date(timeIntervalSinceNow: delay)

    let dateComponents = Calendar.current.dateComponents([.year,.month,.day,.hour,.minute,.second], from: dateAfterDelay)


    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    let identifier = "identifier" + "\(NotificationManager.incrementor)"

    let localNotification = UNNotificationRequest(identifier: identifier, content: notificationContent, trigger: trigger)

    UNUserNotificationCenter.current().add(localNotification){ (error : Error?) in
        if let theError = error {
            print("the error is \(theError.localizedDescription)")
        }
    }
}

Upvotes: 0

Views: 999

Answers (1)

Arnav
Arnav

Reputation: 688

Nothing wrong with your code :

Like you wrote in your question, this is mentioned in the Apple Docs:

If you are sending multiple notifications to the same device or 
computer within a short period of time, the push service will send only 
the last one.

https://developer.apple.com/library/content/technotes/tn2265/_index.html#//apple_ref/doc/uid/DTS40010376-CH1-TNTAG23

Upvotes: 2

Related Questions