Skip to content

How to display messages when app is running in the foreground?

Alexander Boldyrev edited this page Oct 31, 2024 · 22 revisions

Using built-in Mirror push notification feature

Mobile Messaging SDK has a built-in logic to display Mirror push notifications with a minimum development effort, more details here: Mirror push notifications

Writing custom handler

In order to display incoming messages (both pushed by APNs and pulled from the server) while your application is running in the foreground, you have to:

  1. Implement MMMessageHandlingDelegate protocol and it's method willPresentInForeground(message:withCompletionHandler:), i.e.:

    class MyMessageHandlingDelegate : MMMessageHandlingDelegate {
        func willPresentInForeground(message: MM_MTMessage?, notification: UNNotification, withCompletionHandler completionHandler: @escaping (MMUserNotificationType) -> Void) {
            completionHandler([.alert, .sound]) // the foreground notification will be displayed on top of the screen with sound
        }
    }
  2. Pass the delegate object to MobileMessaging SDK:

    MobileMessaging.messageHandlingDelegate = MyMessageHandlingDelegate()
Clone this wiki locally