Skip to content

Managing badge number for seen messages

Andrey Kadochnikov edited this page Feb 8, 2019 · 2 revisions

Manual setting badge number

By design, MobileMessaging.setSeen(...) does not decrement the badge number - it only marks the specific messages as seen and persists the actual seen status. In order to maintain the badge number, you need to explicitly set it as follows:

MobileMessaging.badgeNumber = <new value>

messagesCountersUpdateHandler

There is more convenient way to maintain badge number though. When using Default Message Storage you can leverage a special handler MMDefaultMessageStorage.messagesCountersUpdateHandler that will be called each time the message set as seen (MobileMessaging.setSeen(...) does this as mentioned before). The handler will pass total number of messages along with non-seen counter. Inside the handler you can set badge number:

MobileMessaging.defaultMessageStorage?.messagesCountersUpdateHandler = { total, nonSeen in
    MobileMessaging.badgeNumber = nonSeen
}

You may also want to mark all the storage messages as seen, the most efficient way would be as follows:

MobileMessaging.defaultMessageStorage?.findNonSeenMessageIds { (messageIds) in
    MobileMessaging.setSeen(messageIds: messageIds)
}
Clone this wiki locally