I am scheduling new notifications in iOS10, like this:
func scheduleNotification (event : Meeting, todaysBadgeCounter: Int) {
if #available(iOS 10.0, *)
For Swift 3.0
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("** willPresent")
completionHandler([.badge, .alert, .sound])
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("** didReceive")
completionHandler()
}
You are using incorrect function signatures
The correct function signatures in swift are:
func userNotificationCenter(UNUserNotificationCenter, willPresent: UNNotification, withCompletionHandler: (UNNotificationPresentationOptions) -> Void) {
//your code here
}
and
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
//your code here
}
According to the UNUserNotificationCenterDelegate docs:
Important
You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching. For example, in an iOS app, you must assign it in the applicationWillFinishLaunching(:) or applicationDidFinishLaunching(:) method.
So, it could be an issue of setting the Notification Centre delegate too late.
I have found the answer for this. Below delegate method is called while running app on Xcode 8 with Swift 2.3 and minimum deployment target as iOS 9.3.
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)
In swift 3.0 Use,
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
Reference: https://developer.apple.com/reference/usernotifications/unusernotificationcenterdelegate/1649501-usernotificationcenter
Check to make sure that only your AppDelegate is set as the UNUserNotificationCenter Delegate.
Are you using...
UNUserNotificationCenter.current().delegate = self
... more than once? I was trying to catch notifications with different outcomes in all of my view controllers by changing the delegate in each one and having each use this function:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
// code
}
The issue for me was that I hadn't implemented their functionality yet, and so the original userNotificationCenter function "didReceive" in my AppDelegate wasn't being called. This may be why yours isn't getting called.
Make sure your AppDelegate implementing UNUserNotificationCenterDelegate
protocol.
For Swift
let center = UNUserNotificationCenter.current()
center.delegate = self
For Objective-c
//set delegate to self
[[UNUserNotificationCenter currentNotificationCenter] setDelegate:self];
Assigning delegate to self will trigger following methods.
// App in foreground
private func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
}
//On Action click
private func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
print("didReceive")
}