4

I'm trying to understand why the local notifications is not being displayed in the foreground. I believe I added all the correct code to allow this ability but no banner is showing when my app is in the foreground.

Here's what I added in AppDelegate.swift:

import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
{
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in

            if willAllow == true
            {
                // Do something
            }

        }

        UNUserNotificationCenter.current().delegate = self

        // Override point for customization after application launch.
        return true
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
    {
        print("GO IN HERE")
        completionHandler(.alert)
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
    {

    }

}

My ViewController.swift:

override func viewDidLoad()
{
    super.viewDidLoad()

    let content: UNMutableNotificationContent = UNMutableNotificationContent()
    content.sound = UNNotificationSound.default()
    content.subtitle = "This is a test"

    let trigger: UNTimeIntervalNotificationTrigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)

    let request: UNNotificationRequest = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)

    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}

When my app launches for the first time, I do see a permissions alert to allow my app to push notifications, but I don't see any notifications at all.

I do see the log output GO IN HERE, so willPresent is being called.

Is there something else that I'm missing?

Pangu
  • 3,721
  • 11
  • 53
  • 120
  • please take a look on this http://stackoverflow.com/questions/39893300/how-to-show-local-notification-when-app-is-foreground – arunjos007 Apr 05 '17 at 04:48

4 Answers4

19
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
    
    let center = UNUserNotificationCenter.current()
    center.delegate = self
    
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound]) {(accepted, error) in
        if !accepted {
            print("Notification access denied")
        }
    }
}

Add this in AppDelegate:

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    
    completionHandler( [.alert, .badge, .sound])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    completionHandler()
}
Akash Shindhe
  • 558
  • 4
  • 16
2

In reference to the following answer: UserNotification is not showing (iOS 10)

The reason why my local notifications was not presented in the foreground is not because I "will never see the notifications in the foreground" as mentioned by the previous answer, but because I'm required to set at least the body content, and I did not set that in my code.

By setting content.body = "Some message", I can finally see the notification in the foreground while my app is running.

Community
  • 1
  • 1
Pangu
  • 3,721
  • 11
  • 53
  • 120
  • Thank you. It was driving me crazy why I wasn't seeing local notifications on iOS 10. I was only setting the `title`... (which worked fine on iOS 11 of course) – mathz Feb 09 '18 at 11:05
2

For me it was that I forgot to set the delegate like so:

UNUserNotificationCenter.current().delegate = self
Bogdan Razvan
  • 1,497
  • 1
  • 16
  • 16
-3

You will never see the notifications in the foreground, that's the way they work. If you want to force them to show up, you can create your own UIView and show it or use an existing library https://github.com/pikacode/EBForeNotification

carlos21
  • 485
  • 4
  • 9
  • I'm not following you: `user​Notification​Center(_:​will​Present:​with​Completion​Handler:​)` is `Called when a notification is delivered to a foreground app.` reference: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649518-usernotificationcenter – Pangu Apr 05 '17 at 04:29
  • Yep, it's gonna be called but visually it won't be shown. That's why you would need to show it manually – carlos21 Apr 05 '17 at 05:20