Skip to content

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

Luka Ilić edited this page Nov 21, 2024 · 21 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 a 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 its method willPresentInForeground(message:withCompletionHandler:), for example:

    1.1 Basic Implementation

    class MyMessageHandlingDelegate : MMMessageHandlingDelegate {
        func willPresentInForeground(message: MM_MTMessage?, notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            if #available(iOS 14.0, *) {
                completionHandler([.banner, .sound]) // Shows a banner on top of the screen with sound (.banner option supported from the SDK version '12.16.0')
            } else {
                completionHandler([.alert, .sound]) // Pre-iOS 14 fallback with the same functionality
            }
        }
    }

    1.2 Advanced Implementation

    For more granular control over notification presentation, you can check the message content and customize the notification presentation options:

     class MyMessageHandlingDelegate: NSObject, MMMessageHandlingDelegate {
         func willPresentInForeground(message: MM_MTMessage?, notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
             var presentationOptions: UNNotificationPresentationOptions = []
         
             // Check if the notification should be displayed only in the Notification Center
             if message?.customPayload?["displayInNotificationCenter"] != nil { // Any key-value pair can be put in the customPayload when sending a push from the Infobip platform. 'displayInNotificationCenter' is just an example of how it can be done.
                 if #available(iOS 14.0, *) {
                     presentationOptions.insert(.list) // .list option supported from the SDK version '12.16.0'
                 }
             }
         
             // Check if the notification should be displayed both as a banner and in the Notification Center
             if message?.inAppStyle == .Banner {
                 if #available(iOS 14.0, *) {
                     presentationOptions.insert([.banner, .list]) // .banner and .list option supported from the SDK version '12.16.0'
                 } else {
                     presentationOptions.insert(.alert)
                 }
             }
         
             // Add default options if needed
             presentationOptions.insert([.badge, .sound])
         
             completionHandler(presentationOptions)
         }
     }
  2. Pass the delegate object to the MobileMessaging SDK:

    MobileMessaging.messageHandlingDelegate = MyMessageHandlingDelegate()

Note: Starting with iOS 14, Apple deprecated .alert in favor of:

  • .banner: Shows a temporary banner at the top of the screen
  • .list: Adds the notification to the Notification Center

For more details, see Apple's UNNotificationPresentationOptions documentation

Clone this wiki locally