Push Notifications in iOS

Show Push Notifications when app running in the foreground iOS Swift

(Last Updated On: September 25, 2021)

Push notifications are a nice way to inform your users about the offers, user-specific updates or any message that you want to broadcast to all users.

If your app is running, it can be in two states. Either in the background or in the foreground.

Push notifications work well when the app is in the background and you can see the notification banner presented with a sound(if provided) on your iPhone/iPad. But what about when the app is in the foreground?

Apple does not show up push notifications by default when your app is in the foreground state. So what to do? You have to write some code to deal with the situation of your app receiving a notification while it is in the foreground.

If you receive local or remote notifications while your app is running in the foreground, you’re responsible for passing the information to your users in an app-specific way.

Apple

Apple guidelines are clear. You are responsible for handling the push notifications in the foreground state. So how to achieve this? Don’t worry, it’s pretty easy. Below are the steps.

Steps to show Push Notifications in the foreground

Step 1 : Add delegate UNUserNotificationCenterDelegate in AppDelegate class.

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

Step 2: Set the UNUserNotificationCenter delegate in didFinishLaunchingWithOptions

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().delegate = self
        return true
    }

Step 3: This step will allow your app to show Push Notification even when your app is in the foreground

  
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        print(userInfo) // the payload that is attached to the push notification
        // you can customize the notification presentation options. Below code will show notification banner as well as play a sound. If you want to add a badge too, add .badge in the array.
        completionHandler([.alert,.sound])
    }

Step 4: If you want to perform an action with the click of a push notification, you can do it using the below method

   
    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                   didReceive response: UNNotificationResponse,
                                   withCompletionHandler completionHandler: @escaping () -> Void) {
           let userInfo = response.notification.request.content.userInfo
           // Print full message.
           print("tap on on forground app",userInfo)
           completionHandler()
       }
    

Well, that was it! You have successfully configured push notifications to receive them in the foreground state too.

It was EASY, right? Do let me know in the comments.

Also, do not forget to become a subscriber by entering your email below so that you can receive the tutorials directly in your inbox.

Namaste!

6 Comments

Leave a Comment