ios - 更新到 swift 4 后 UNUserNotification 不起作用

标签 ios swift swift3 swift4 unusernotificationcenter

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

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”并且没有继续。

最佳答案

response.actionIdentifier == "answer1"仅当您点击“谢谢!”时才为 true通知中的选项。如果您只是点击通知,情况就不会如此。请检查

关于ios - 更新到 swift 4 后 UNUserNotification 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812413/

相关文章:

javascript - 区分 iPad 和 .来自网络应用程序内部的 iPhone?

ios - 用随机位置的按钮填充 View

ios - Swift encodeObjectForKey - 用 nil 更新值

ios - 无法调用非函数类型的值 '((AnyObject) -> AnyObject?)!' - Swift 3

ios - 使用 UISearchResults 过滤时出现问题

javascript - react-native-scrollable-tab-view 上的无形边距

ios - 永远不会调用 UITextField drawTextInRect

swift - 错误: binary operator '+=' cannot be applied to two '[Int : String]' operands

iOS 自动使用自定义阿拉伯语/西里尔语字体

arrays - 将自定义数组保存到 Core Data 中