如何向自定义通知添加按钮并相应地响应按钮?

时间:2017-09-21 01:13:55

标签: ios swift uilocalnotification

我使用UNNotificationContentExtension并尝试弄清楚如何在我的视图中添加按钮,并在我的扩展类中为该按钮设置IBAction?

请参阅附件图片:

enter image description here

正如你所看到的,我已经在UNNotificationContentExtension课程中添加了两个按钮,但是永远不会被调用?

这是我在我的app delegate中设置通知的方式:

func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - >布尔{

let center = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert]

center.requestAuthorization(options: options) { (authorized, error) in
    if authorized {
        let categoryId = "com.tutplus.Custom-Notification-Interface.notification"

        let category = UNNotificationCategory(identifier: categoryId, actions: [], intentIdentifiers: [], options: [])
        center.setNotificationCategories([category])

        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 30, repeats: false)
        let content = UNMutableNotificationContent()
        content.categoryIdentifier = categoryId
        content.title = "Notification Title"
        content.subtitle = "Notification Subtitle"
        content.body = "Notification body text"
        content.userInfo = ["customNumber": 100]

        let request = UNNotificationRequest(identifier: "exampleNotification", content: content, trigger: trigger)
        center.add(request, withCompletionHandler: nil)
    }
}

return true

}

我不想以这种方式将这些添加到UNNotificationAction

let ok = UNNotificationAction(identifier: "OKIdentifier",
                              title: "Turn off", options: [.destructive])

我需要在视图中放置按钮并相应地对它们做出反应。如果有人能帮助我,我感激不尽。

0 个答案:

没有答案