How to Get Push Notification while iOS App is in Foreground
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.
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.
AI Grammar: Correct grammar, spell check, check punctuation, and parphrase.
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.
- Set a delegate for the
UNUserNotificationCenter
. - 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.
- Make our
AppDelegate
conform toUNUserNotificationCenterDelegate
protocol.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
}
- Then, assign
AppDelegate
as a delegate ofUNUserNotificationCenter
. I do this inapplication(_: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 ShareBest Free Fonts for iOS app
A curated list of websites which you can find a free font for your iOS app.