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

后端 未结 9 1202
春和景丽
春和景丽 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 01:03

    Use belwo delegate method for Swift 2.3:

    func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void)
    
    0 讨论(0)
  • 2020-12-13 01:05

    Request identifier is not the notification category.

    Just add this line:

    content.categoryIdentifier = identifier
    

    Update: Just made a simple app. Everything seems to working fine:

    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
        UNUserNotificationCenter.current().delegate = self
    
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
            if granted {
                self.registerCategory()
                self.scheduleNotification(event: "test", interval: 3)
                self.scheduleNotification(event: "test2", interval: 5)
            }
        }
    
        return true
    }
    
    func registerCategory() -> Void{
    
        let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
        let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
        let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])
    
        let center = UNUserNotificationCenter.current()
        center.setNotificationCategories([category])
    
    }
    
    func scheduleNotification (event : String, interval: TimeInterval) {
        let content = UNMutableNotificationContent()
    
        content.title = event
        content.body = "body"
        content.categoryIdentifier = "CALLINNOTIFICATION"
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
        let identifier = "id_"+event
        let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
    
        let center = UNUserNotificationCenter.current()
            center.add(request, withCompletionHandler: { (error) in
        })
    
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("didReceive")
        completionHandler()
    }
    
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("willPresent")
        completionHandler([.badge, .alert, .sound])
    }
    
    }
    

    Update 2: Rewritten in Swift 2.3

    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
    var window: UIWindow?
    
    func applicationDidFinishLaunching(application: UIApplication) {
        UNUserNotificationCenter.currentNotificationCenter().delegate = self
        UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
            if granted {
                self.registerCategory()
                self.scheduleNotification("test", interval: 3)
                self.scheduleNotification("test2", interval: 5)
            }
        }
    }
    
    
    func registerCategory() -> Void{
    
        let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
        let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
        let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])
    
        let center = UNUserNotificationCenter.currentNotificationCenter()
        center.setNotificationCategories([category])
    
    }
    
    func scheduleNotification(event : String, interval: NSTimeInterval) {
        let content = UNMutableNotificationContent()
    
        content.title = event
        content.body = "body"
        content.categoryIdentifier = "CALLINNOTIFICATION"
        let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
        let identifier = "id_"+event
        let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
    
        let center = UNUserNotificationCenter.currentNotificationCenter()
        center.addNotificationRequest(request) { (error) in
    
        }
    }
    
    func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
    
        print("willPresent")
        completionHandler([.Badge, .Alert, .Sound])
    }
    
    func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
    
        print("didReceive")
        completionHandler()
    }
    
    }
    
    0 讨论(0)
  • 2020-12-13 01:06

    The docs says to set the delegate in applicationWillFinishLaunching(:) or applicationDidFinishLaunching(:). So include the following code in AppDelegate:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       UNUserNotificationCenter.current().delegate = self
    }
    

    After this delegate is being set, the following willPresent function will be called.

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
       print("willPresent")
       completionHandler([.alert, .sound])
    }
    
    0 讨论(0)
提交回复
热议问题