1

我目前在 iOS 10 上查看时间间隔通知上的操作按钮时遇到问题。我创建了两个 UNNotificationAction 对象“确定”和“取消”添加为通知类别。

我在模拟器上收到通知,没有任何操作按钮。下面是我的代码。

let ok = UNNotificationAction(identifier: "OKIdentifier",
                                      title: "OK", options: [])

let cancel = UNNotificationAction(identifier: "CancelIdentifier",
                                  title: "Cancel",
                                  options: [])

let category = UNNotificationCategory(identifier: "message",
                                      actions: [ok, cancel],
                                      minimalActions: [ok, cancel],
                                      intentIdentifiers: [],
                                      options: [])

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

let content = UNMutableNotificationContent()
content.title = contentTitle
content.subtitle = contentSubtitle
content.body = contentBody

let model: TimeIntervalNotificationModel = notificationsModel as!  TimeIntervalNotificationModel

trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: model.timeInterval!, repeats: notificationsModel.repeats) as UNTimeIntervalNotificationTrigger

let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request){(error) in
                if (error != nil){
                    //handle here
         print("Error: Adding notification failed:\(error?.description)")

         self.delegate?.didFailToAddNotification(error: error!)
                }
            }
4

1 回答 1

11

修复了问题。忘记设置通知内容的类别标识符属性。

content.categoryIdentifier = "message"

定义:

应用程序定义的类别对象的标识符指定类别标识符以创建可操作的通知。标识符必须属于您之前在应用中注册的 UNNotificationCategory 对象。当通知传递给用户时,系统将在该类别对象中定义的动作适当地添加到通知界面。

于 2016-07-21T09:29:26.867 回答