Swift:带操作按钮的区域设置通知

时间:2018-11-18 15:01:56

标签: swift notifications

您好,我想向我的UNMutableNotification添加按钮操作,但是我不明白为什么它不起作用,但我还是将catergoryIdentifier传递给了他。

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

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

我的代码:

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

}

0 个答案:

没有答案