这可能是因为操作按钮的显示时间被忽略了。使用UNNotificationAction来创建您的操作,并确保将它们分配给UNNotificationCategory。注意,将交互式通知添加到未执行的通知中不会显示操作按钮。 以下是示例代码:
// 创建操作 let snoozeAction = UNNotificationAction(identifier: "SNOOZE_ACTION", title: "Snooze", options: UNNotificationActionOptions(rawValue: 0)) let completeAction = UNNotificationAction(identifier: "COMPLETE_ACTION", title: "Complete", options: .foreground)
// 创建类别,并将操作分配给类别 let todoCategory = UNNotificationCategory(identifier: "TODO_CATEGORY", actions: [snoozeAction, completeAction], intentIdentifiers: [], options: [])
// 注册类别 let center = UNUserNotificationCenter.current() center.setNotificationCategories([todoCategory])
// 创建通知,并将类别分配给通知 let content = UNMutableNotificationContent() content.title = "Get back to work" content.body = "Complete the tasks in the TODO list" content.categoryIdentifier = "TODO_CATEGORY"
// 发送通知 let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false) let request = UNNotificationRequest(identifier: "TODO_ITEM", content: content, trigger: trigger) center.add(request, withCompletionHandler: nil)