iOS
push notifications
app development
foreground notifications
iOS development

Get push notification while App in foreground iOS

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

On iOS, push notifications are not automatically shown to the user in the same way when the app is already in the foreground. If you want a banner, sound, or badge while the app is active, your app must opt in by implementing the notification-center delegate callback for foreground presentation.

So the problem is usually not receiving the push. It is deciding how the system should present it while the app is already open.

Request Permission and Set the Delegate

First, request notification authorization and assign a UNUserNotificationCenterDelegate:

swift
1import UIKit
2import UserNotifications
3
4@main
5class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
6    func application(
7        _ application: UIApplication,
8        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
9    ) -> Bool {
10        let center = UNUserNotificationCenter.current()
11        center.delegate = self
12
13        center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
14            print("granted=\(granted) error=\(String(describing: error))")
15        }
16
17        application.registerForRemoteNotifications()
18        return true
19    }
20}

Without the delegate assignment, foreground presentation customization will not happen in your app code.

Present Notifications While the App Is Active

The key method is userNotificationCenter(_:willPresent:withCompletionHandler:):

swift
1func userNotificationCenter(
2    _ center: UNUserNotificationCenter,
3    willPresent notification: UNNotification,
4    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
5) {
6    completionHandler([.banner, .sound, .badge, .list])
7}

That completion handler tells iOS which presentation options to use while the app is in the foreground.

If you omit this method, the notification may arrive, but the system usually will not show the normal foreground presentation you are expecting.

Older and Newer Presentation Options

Modern iOS versions use options such as .banner and .list. In older code, you may see .alert instead.

The intent is the same: the app tells the system whether foreground notifications should be displayed visibly, audibly, or only handled silently in code.

So if you are maintaining older examples, do not be surprised by the difference in option names.

When You Might Not Want a Banner

Sometimes the best foreground behavior is not to show a system banner at all. If the user is already looking at the relevant screen, a custom in-app update may be better.

For example, a chat app might update the conversation view directly while the user is already in that chat. In that case, you can still receive the notification but decide not to request banner presentation.

That means foreground notification handling is partly a UX decision, not just a delivery problem.

Remote Push Still Requires the Usual Setup

Foreground presentation is only one piece of the overall push flow. You still need the normal push-notification requirements in place:

  • notification permissions
  • remote notification registration
  • valid APNs configuration
  • correct payload from the server side

If those pieces are broken, implementing the foreground delegate method alone will not make notifications work.

Common Pitfalls

The biggest mistake is forgetting to set UNUserNotificationCenter.current().delegate. Without that, the foreground callback will not be routed to your code.

Another common issue is implementing the delegate method but not calling the completion handler with presentation options.

A third problem is confusing receipt with display. The app may receive the notification data even when the system is not showing a banner.

Finally, choose presentation carefully. Showing a system alert while the user is already in the active screen can be noisy if an in-app update would be better.

Summary

  • To show push notifications in the foreground on iOS, set a UNUserNotificationCenterDelegate.
  • Implement userNotificationCenter(_:willPresent:withCompletionHandler:).
  • Return the presentation options you want, such as .banner, .sound, .badge, or .list.
  • Foreground notification handling is a UX choice as well as a technical one.
  • Delegate wiring does not replace the normal APNs setup required for remote notifications.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.