Foreground Notifications
    • 2 Minutes to read
    • Dark
      Light

    Foreground Notifications

    • Dark
      Light

    Article summary

    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

    1. Enable Foreground Notifications: Conform to the UNUserNotificationCenterDelegate and implement the following method:

      For Objective-C:

      - (void)userNotificationCenter:(UNUserNotificationCenter *)center 
             willPresentNotification:(UNNotification *)notification 
               withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
      
          completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionBadge);
      }

      For Swift:

      func userNotificationCenter(_ center: UNUserNotificationCenter, 
                                  willPresent notification: UNNotification, 
                                  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          completionHandler([.alert, .sound, .badge])
      }
    2. Customize Foreground Notifications: Modify the presentation style based on the app's context or user settings:

      func userNotificationCenter(_ center: UNUserNotificationCenter, 
                                  willPresent notification: UNNotification, 
                                  withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          if notification.request.content.categoryIdentifier == "IMPORTANT" {
              completionHandler([.alert, .sound])
          } else {
              completionHandler([])
          }
      }
    3. Interactive Foreground Notifications:

      • Add custom actions to notifications for enhanced user engagement:

      let acceptAction = UNNotificationAction(identifier: "ACCEPT_ACTION",
                                              title: "Accept",
                                              options: [.foreground])
      let declineAction = UNNotificationAction(identifier: "DECLINE_ACTION",
                                               title: "Decline",
                                               options: [])
      
      let category = UNNotificationCategory(identifier: "INVITE_CATEGORY",
                                            actions: [acceptAction, declineAction],
                                            intentIdentifiers: [],
                                            options: [])
      UNUserNotificationCenter.current().setNotificationCategories([category])
      • Assign the category to the notification:

      let content = UNMutableNotificationContent()
      content.categoryIdentifier = "INVITE_CATEGORY"

    Keep in mind:

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


    Was this article helpful?

    What's Next