0

I have checked that all the notifications are scheduled correctly at the NotificationCenter. And, I have the completionHandler([.alert, .badge, .sound]) code in my app delegate to make sure the notifications will show up even when the user is using the app.

The result is notifications will show only the app is in the background, not the other way.

Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22
Nick Chang
  • 1
  • 1
  • 4
  • Did you handle receiving notifications (when the app is active) in AppDelegate? – Artem Kirillov Aug 31 '17 at 02:48
  • You can check this link - https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3 I hope this will work for you. – Nupur Gupta Sep 28 '17 at 05:31
  • Please check this link - https://stackoverflow.com/questions/39713605/getting-local-notifications-to-show-while-app-is-in-foreground-swift-3 – Nupur Gupta Sep 28 '17 at 05:32

2 Answers2

0

Did you handle the local notifications in app delegate like this?

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    // Do something you want
    println("Received Local Notification:")
    println(notification.alertBody)
}

Above is for local notifications and there is another method for hadle remote notifications,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Do something you want
}

Make sure you are handling the local notifications using local notification method.

Bishan
  • 401
  • 1
  • 9
  • 16
  • yes! I did. I use: func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { – Nick Chang Aug 31 '17 at 04:49
0

Please try this:

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound])
    }
}

You can find Apple doc here.

What I have understood is, whatever options I send to completionHandler will happen.

If you send just .alert, it will just show the alert If you send just .sound, it will just play the sound specified by notification.

And will do both if we send both options.

And will do nothing if we don't send any parameter.

So what you need to do is send parameter as .alert.

In didFinishLaunchingWithOptions you will also need to add

UNUserNotificationCenter.current().delegate = self
Nikhil Manapure
  • 3,748
  • 2
  • 30
  • 55