0

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)")
        }
    }
}
osebas15
  • 61
  • 6
  • As mentioned by Arnav this is expected functionality, so now the question becomes, what is a good workaround? – osebas15 Sep 12 '17 at 04:02
  • The problem was that I was trying to send the notifications from the app even though the app had become inactive. It was resolved by realizing this and queueing up all of the upcoming notifications before the app actually lost active status – osebas15 Sep 14 '17 at 00:29

1 Answers1

1

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

Arnav
  • 668
  • 6
  • 14
  • thank you for your response and the link, maybe you could point me to a workaround? – osebas15 Sep 12 '17 at 04:02
  • I wonder how apps like iMessage, WhatsApp and the many other apps work without running into this issue. Or is that they queue these things with a minimum 2-3 second interval? – mfaani Feb 24 '20 at 23:27