简体   繁体   中英

UNUserNotification doesn't work after updating to swift 4

So I had an interacitve push notification but it suddenly stopped working, this is my code:

func pushNotification(){
    let content = UNMutableNotificationContent()
    let answer1 = UNNotificationAction(identifier: "answer1", title: "thank you!", options: UNNotificationActionOptions.foreground)

    let category = UNNotificationCategory(identifier: "myCategory", actions: [answer1, answer2], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])
    content.sound = UNNotificationSound.default()
    //Created notification
    content.body = "how are you?"
    content.categoryIdentifier = "myCategory"
    content.badge = 1
    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 10, repeats: false)
    let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}



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

        if response.actionIdentifier == "answer1"
        {
            print("ok")
            performSegue(withIdentifier: "FastResult", sender: self)
        }

        completionHandler()
    }

when I run the app I get the notification but I doen't recive the output: "ok" and there is no segue.

response.actionIdentifier == "answer1" only will be true when you tap on "thank you!" option in the notification. It wont be true if you just tap the notification. please check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM