UNUserNotificationCenterDelegate doesn't work with local notification

I am using userNotificationCenter(_:willPresent:withCompletionHandler:) to get foreground notification. However, it seems this method does not work with local notIfication?


The notification shows when app is in background or the device is locked. But it never shows when the app is in foreground and userNotificationCenter(_:willPresent:withCompletionHandler:) never runs.


I have paste my code but I can't pass the "The message contains invalid characters.". Sigh...


Any idea?


Xcode 8 beta 6, iOS 10 developer beta 7

the same for me.


in appdelegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let center = UNUserNotificationCenter.current()

center.delegate = self

return true

}


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

print("willPresent")

}


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

print("didReceive")

}


I trigger in this way:


(sample from APPLE)


func schedule() {

let content = UNMutableNotificationContent()


content.title = "Hello!"

content.body = "Hello_message_body"

content.sound = UNNotificationSound.default()


content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)

content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)

/

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

let request = UNNotificationRequest.init(identifier: "FiveSecond",

content: content,

trigger: trigger)


/

let center = UNUserNotificationCenter.current()

center.add(request)

}


I can see but no call back.

+1 this is also broken for me. Not getting any activations of these methods. Delegate is assigned before app finishes loading (as per docs). Nothing.

Does work for me in beta 7 with local notifications while switched to foreground mode.

Don't forget to call the completionHandler() in willPresentNotification.

Does not work for me too.

Same for me too. The methods not receive the events. I still having problems with UNUserNotificationCenterDelegate in AppDelegate. I added the framewofk UserNotifications to my project writted the import into AppDelegate, but not find the delegate class.

yap same here the delegate method never get called, instead the "application(_ application: UIApplication, didReceive notification: UILocalNotification)" get called

I'm also seeing some weird behavior. I am unable to receive any UNNotificationAction's as a result of "didReceive" delegate not being called. If someone has this working, can you share the exact code that works for setting up UNUserNotificationCenterDelegate?

I got this working. try this in your app Delegate (Swift 3):


@available(iOS 10.0, *)

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

@available(iOS 10.0, *)

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

I can confirm that the code suggested by Xcode (beta 6) doesn't work but the one posted by Whootang12 works really well so the

@escaping

is really important.

it sure helps me, i wonder is going to be this way permanently or is it gonna change in the next beta

I have posted answer here: http://stackoverflow.com/a/39600518/1140780

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

Thank you Whootang12, but unfortunately this doesn't work for me...the weird behavior is still happening : the delegate method never get called, instead the "application(_ application: UIApplication, didReceive notification: UILocalNotification)" get called instead.. 😟

I have copied the code, with @escaping before the param, I am calling the completion handler, etc.

I cannot get a breakpoint to work, and it seems that the function "willPresent" never gets called.



@available(iOS 10.0, *)

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

completionHandler([.alert, .sound])

}


Notifications work on the home screen, so I gather my registration is OK.

Anyone has any ideas??

UNUserNotificationCenterDelegate doesn't work with local notification
 
 
Q