- 1 Minute to read
- Print
- DarkLight
Foreground Notifications
- 1 Minute to read
- Print
- DarkLight
Overview
Foreground notifications in iOS allow apps to display notifications while they are active. These notifications ensure that important updates are delivered without moving the app to the background.
Use Case
Purpose:
Display real-time updates, alerts, or actions while the app is being used.
Provide a seamless and engaging user experience with in-app notifications.
Benefits:
Ensure visibility of important updates during app usage.
Allow users to respond to notifications without switching context.
Implementation Details
Enable Foreground Notifications: It is possible to read the showNotificationsOnForeground property so the user can know whether it needs to show a notification in the foreground or not. Conform to the
UNUserNotificationCenterDelegate
and implement the following method:For Objective-C:
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { if ([[Appoxee shared] showNotificationsOnForeground]) { if (completionHandler) completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); } else { if (completionHandler) completionHandler(UNNotificationPresentationOptionNone); } }
For Swift:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler([.alert, .sound, .badge]) }
Take full control of unnotification center: If you want to take full control of UNNotification center you need to pass results back to Mapp Engage SDK like this:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { if (Appoxee.shared()?.showNotificationsOnForeground ?? false) { completionHandler([.alert, .sound, .badge]) } else { completionHandler([]) } }
Foreground notifications require explicit handling; otherwise, they won’t appear by default in iOS.
Ensure notifications respect user preferences, such as disabling sounds or alerts when requested.
Test notification behavior in different app states (foreground, background, and terminated).