【问题标题】:UNUserNotification doesn't work after updating to swift 4更新到 swift 4 后,UNUserNotification 不起作用
【发布时间】:2023-12-15 09:50:01
【问题描述】:

所以我有一个交互式推送通知,但它突然停止工作, 这是我的代码:

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()
    }

当我运行应用程序时,我会收到通知,但我没有收到输出:“ok”并且没有 segue。

【问题讨论】:

    标签: ios swift swift3 swift4 unusernotificationcenter


    【解决方案1】:

    response.actionIdentifier == "answer1" 只有在您点击“谢谢!”时才会为真。通知中的选项。如果您只是点击通知,那将不是真的。请检查

    【讨论】:

    • 检查过了,不知为什么我按下它也不起作用
    • 我只给 : let answer1 = UNNotificationAction(identifier: "answer1", title: "thank you!", options: UNNotificationActionOptions.foreground) let category = UNNotificationCategory(identifier: "myCategory", actions: [answer1],intentIdentifiers:[],选项:[])。
    最近更新 更多