Show iOS notification when app is running

During our last competence activity we took a look at iOS notification. There has been some changes and a few new things added in iOS 10. To learn what is new I recommend to see the wwdc videos about notification. I did also watch some videos on YouTube and read some blog posts, links below.

My simple lab can be found here on GitHub. There is a solution branch where you can find a solution. There is also a powerpoint in there but it mostly contains the links above. Look at how the first example works and then try to solve the others.

Now back to the title. During the lab we discussed a lot of thing but one thing stood out, show notification when app is running. Many said this has been difficult in the past and all kind of workaround has been tried to solve this. To get this going one need to set UNUserNotificationCenter.current().delegate = self in didFinishLaunchingWithOptions. I did it in the AppDelegate.swift but I guess it can be set in any view.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        // Add this to get notification when app is in use
        UNUserNotificationCenter.current().delegate = self

This is not enough. You also need to tell the notification what to do with it when you get it. This is done in the extension part, also in AppDelegate.swift.

// Extension tell app to be able to get notification when in use and also for extensions
extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

This will make the notification show when the app is running.

Happy coding

This Post Has One Comment

  1. Fernando Perez

    Thanks!. It works!

Leave a Reply