UNUserNotificationCenter did receive response with completion handler is never called iOS10, swift 2.3

后端 未结 9 1201
春和景丽
春和景丽 2020-12-13 00:03

I am scheduling new notifications in iOS10, like this:

func scheduleNotification (event : Meeting, todaysBadgeCounter: Int) {

    if #available(iOS 10.0, *)         


        
相关标签:
9条回答
  • 2020-12-13 00:44

    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()
    }
    
    0 讨论(0)
  • 2020-12-13 00:47

    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
    
    }
    
    0 讨论(0)
  • 2020-12-13 00:48

    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.

    0 讨论(0)
  • 2020-12-13 00:52

    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

    0 讨论(0)
  • 2020-12-13 00:58

    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.

    0 讨论(0)
  • 2020-12-13 01:02

    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")
          }
    
    0 讨论(0)
提交回复
热议问题