How to Get Push Notification while iOS App is in Foreground

⋅ 3 min read ⋅ Notification

Table of Contents

By default, if an iOS app gets a notification while the app is in the Foreground, the notification banner won't show up.

A notification banner show only when an app isn't active.
A notification banner show only when an app isn't active.

Since iOS 10, we can override this behavior and make a notification show even when an app is active. Let's learn how to do it.

Why does Push Notification not show up when an app is in the Foreground

Before iOS 10, if a notification arrived while an app was in the foreground, the system would silence that notification.

Apple thinks the default notification banner is redundant since a user is already in the app.

They want us to update our app's interface directly. For example, if a new message arrives, you need to update the badge count in your tab bar.

Since iOS 10, Apple has become less opinionated about this behavior and allows us to decide whether to

  • Silence the incoming notification (the same behavior as before iOS 10) or
  • Tell the system to continue to display the notification interface

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

How to Show Notification Banner while iOS App is in the Foreground

To make a notification banner continue to display while an app is in the foreground, you need to do two things.

  1. Set a delegate for the UNUserNotificationCenter.
  2. Implement the userNotificationCenter(_:willPresent:withCompletionHandler:) method.

Set a delegate for the UNUserNotificationCenter

First, we need to set a delegate for the UNUserNotificationCenter.

In this article, I will keep it simple and use AppDelegate as a UNUserNotificationCenter's delegate. To do that, we need to do the following steps.

  1. Make our AppDelegate conform to UNUserNotificationCenterDelegate protocol.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

}
  1. Then, assign AppDelegate as a delegate of UNUserNotificationCenter. I do this in application(_:didFinishLaunchingWithOptions:).
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}

Implement the delegate method

After that we need to implement userNotificationCenter(_:willPresent:withCompletionHandler:) method, which is a method of UNUserNotificationCenterDelegate.

userNotificationCenter(_:willPresent:withCompletionHandler:) allow us to handle a notification that arrived while the app was running in the foreground.

In this method, you can take any actions to process the notification and update your app interface.

Once finished, you call the completionHandler block and specify how you want the system to alert the user.

In the following example, we specify .banner to make an app display a notification banner while the app is in the foreground.

func userNotificationCenter(
_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
// Update the app interface directly.

// Show a banner
completionHandler(.banner)
}

That's all you need to do to make a notification banner show within an app.


Read more article about Notification or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Best Free Fonts for iOS app

A curated list of websites which you can find a free font for your iOS app.

Next
How to change List Row separator color in SwiftUI

Learn how to colorize a list row separator.

← Home