Swift:将操作按钮设置为本地通知

时间:2018-11-19 07:16:50

标签: swift notifications

你好,我想在我的UNMutableNotification中添加一个按钮动作,但是我不明白为什么它不起作用,但我还是给他传递了catergoryIdentifier。

我的代码:

class LocalNotification: NSObject {

    class func createToDate(identifier: String,
                            title: String,
                            subtitle: String = "",
                            body: String,
                            to date: Date,
                            sound: UNNotificationSound = .default,
                            repeatNotification: Bool = false,
                            completion: @escaping (_ error: Error?) -> ()) {

        let content = UNMutableNotificationContent()
        content.title = title
        content.subtitle = subtitle
        content.body = body
        content.sound = sound
        content.categoryIdentifier = "fittoCateroy"

        let dateComponent = Calendar.current.dateComponents([.day, .hour, .minute], from: date)
        let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: repeatNotification)
        let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request) { (error) in
            completion(error)
        }
    }

    class func withAction() {
        let snoozeAction = UNNotificationAction(identifier: "snooze", title: "Snooze", options: [])

        let category = UNNotificationCategory(identifier: "fittoCateroy", actions: [snoozeAction], intentIdentifiers: [], options: [])

        UNUserNotificationCenter.current().setNotificationCategories([category])

        createToDate(identifier: "M",
            title: "My title", subtitle: "Sub", body: "Body",
            to:Date().adding(seconds: 10), sound: .default, repeatNotification: false) { error in

            if let error = error {
                print("ERRRRROR \(error.localizedDescription)")
            }
        }
    }

}

当我从控制器调用函数createToDate()时,通知起作用,但是,如果我调用函数withAction(),则通知不起作用。

Date().adding(second: 10)是自定义扩展名

0 个答案:

没有答案
相关问题