-
Notifications
You must be signed in to change notification settings - Fork 21
How to display messages when app is running in the foreground?
Mobile Messaging SDK has a built-in logic to display Mirror push notifications with a minimum development effort, more details here: Mirror push notifications
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:
-
Implement
MMMessageHandlingDelegate
protocol and its methodwillPresentInForeground(message:withCompletionHandler:)
, for example: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 } } }
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) } }
-
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
If you have any questions or suggestions, feel free to send an email to support@infobip.com or create an issue.
- Library events
- Server errors
- Users and installations
- Messages and notifications management
- Inbox
Geofencing serviceDEPRECATED- Privacy settings
- In-app chat
- WebRTC Calls and UI