Foreground Notifications

Prev Next

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

  1. 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.

  2. Benefits:

    • Ensure visibility of important updates during app usage.

    • Allow users to respond to notifications without switching context.

Implementation Details

Conform to UNUserNotificationCenterDelegate and implement userNotificationCenter(_:willPresent:withCompletionHandler:). The SDK exposes the showNotificationsOnForeground flag on Appoxee.shared(); check it to decide whether to display the system banner. When the flag is false, return an empty options set so the notification is delivered silently and your app handles the payload directly.

func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    if Appoxee.shared()?.showNotificationsOnForeground ?? false {
        completionHandler([.alert, .sound, .badge])
    } else {
        completionHandler([])
    }
}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
       willPresentNotification:(UNNotification *)notification
         withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {

    if ([[Appoxee shared] showNotificationsOnForeground]) {
        completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
    } else {
        completionHandler(UNNotificationPresentationOptionNone);
    }
}

Note:

  • 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).