Skip to content

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

Davor Komušanac edited this page Dec 5, 2022 · 22 revisions

Using built-in in-app feature

Mobile Messaging SDK has a built-in logic to display in-app messages with a minimum development effort, more details here: In-app 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.messageHandlindDelegate = MyMessageHandlingDelegate()
Clone this wiki locally