我正在尝试关注this document以便在我的应用中设置一些通知。但这只是部分起作用。
更准确地说,我收到了预期的通知,但没有选择答案(在文档示例中为“接受-拒绝”)。下面是我的代码(相关部分),我使用“ action-One”和“ action-Two”而不是“ accept”和“ Decline”;但是除此之外,我尝试遵循该文档。
构建通知:
let notification = UnmutableNotificationContent()
notification.title = notificationInfo.title
notification.body = notificationInfo.body
notification.sound = UNNotificationsound.default
if let data = notificationInfo.data {notification.userInfo = data}
// Define the custom actions.
let actionOne = UNNotificationaction(identifier: "actION_ONE",title: "action-One",options: UNNotificationactionOptions(rawValue: 0))
let actionTwo = UNNotificationaction(identifier: "actION_TWO",title: "action-Two",options: UNNotificationactionOptions(rawValue: 0))
// Define the notification type
let userReactionCategory =
UNNotificationCategory(identifier: "USER_REact",actions: [actionOne,actionTwo],intentIdentifiers: [],hiddenPreviewsBodyPlaceholder: "",options: .customDismissaction)
// Register the notification type.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.setNotificationCategories([userReactionCategory])
notification.categoryIdentifier = "USER_REact"
return notification
userNotificationCenter:didReceive:withCompletionHandler 函数:
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
let traceRecord = TraceUnit(context: self.moc) // To leave some logs in a DB.
// Perform the task associated with the action.
switch response.actionIdentifier {
case "actION_ONE":
traceRecord.name = "actION_ONE"
break
case "actION_TWO":
traceRecord.name = "actION_TWO"
break
// Handle other actions…
default:
traceRecord.name = "actION_DEFAULT"
break
}
traceRecord.timeStamp = Date()
do {
try self.moc.save()
} catch let error {
// Handle the Core Data error.
print("Can't save as expected!!!!")
print("Error : \(error)")
}
// Always call the completion handler when done.
completionHandler()
}
尝试不同的选项后。我见过调用 userNotificationCenter:didReceive:withCompletionHandler 函数,但是里面的switch块似乎没有执行,甚至是默认部分。
任何人都可以指出我在参考文档中没有正确遵循的内容,以使其无法正常工作吗?