When I first run my app the simulator is able to display notifications whilst the app is in the foreground.
When I relaunch the app didReceive notification:
(i.e. notification method from iOS 9) is called instead of willPresent notification
(i.e. notification method from iOS 10) and no notifications are displayed whilst the app is in the foreground. However, a notification is displayed if the app is in the background.
Neither this question or this question provided a solution to this problem.
I get authorisation for notifications using this code:
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]? = [:]) -> Bool {
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .sound]) { (granted, error) in
UNUserNotificationCenter.current().delegate = self
}
}
return true
}
And I've implemented the willPresent notification method:
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) completionHandler(UNNotificationPresentationOptions.alert)
}
Here's how I create the notification:
let content = UNMutableNotificationContent()
content.title = title
content.subtitle = subtitle
content.body = message
content.categoryIdentifier = identifier
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: false)
let request = UNNotificationRequest(
identifier: "identifier",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(
request, withCompletionHandler: nil)
Problem in a nutshell: How do I ensure the app is able to display a notification when the app is in the foreground after the first time that the app is run?
Thanks in advance.