我在iOS 10上按时间间隔通知查看操作按钮时遇到问题。我创建了两个UNNotificationAction对象' OK' &安培; '取消'添加为通知类别。
我在没有任何操作按钮的情况下在模拟器上收到通知。以下是我的代码。
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!)
}
}
答案 0 :(得分:10)
修正了问题。忘了设置通知内容的类别标识符属性。
content.categoryIdentifier = "message"
定义:
应用程序定义的类别对象的标识符指定类别标识符以创建可操作的通知。标识符必须属于您之前在应用程序中注册的UNNotificationCategory对象。当通知发送给用户时,系统会根据需要将该类别对象中定义的操作添加到通知界面。