UserNotifications
。当3D触摸此通知时,我希望它显示一些“警报信息”,以及“暂停”警报的选项,这将在5分钟内有效地重复通知。
这一切都很好,只是当我单击“打盹”按钮时,通知没有被取消。
NotificationContentExtension
要修改内容,请使用categoryIdentifier
"ReminderNotificationExtensionCategory"
。然后我在代码中创建了我的类别,如下所示:
let snooze = UNNotificationAction(identifier: "snoozeActionIdentifier", title: NSLocalizedString("SNOOZE", comment: ""), options: .foreground)
let reminderCategory = UNNotificationCategory(identifier: "ReminderNotificationExtensionCategory", actions: [snooze], intentIdentifiers: [], options: .customDismissAction)
UNUserNotificationCenter.current().setNotificationCategories([reminderCategory])//Not sure if I should set this every time or just once..
然后创建通知对象
let content = UNMutableNotificationContent()
content.categoryIdentifier = "ReminderNotificationExtensionCategory"
content.title = "test"
content.body = "test"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) //Fire in 3 seconds
let request = UNNotificationRequest(identifier: "someIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error:Error?) in /**/}
现在,我的通知将在3秒内发送。
UIView
UNNotificationContentExtension
将调用委托方法,特别是
func didReceive(_ response: completion:)
完成仅接受值
UNNotificationContentExtensionResponseOption
.dismiss
,
.doNotDismiss
.dismissAndForwardAction
.
为了进行测试,我做了以下工作:
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
print("This is happening. ActionIdentifier: ", response.actionIdentifier)
completion(.dismiss)
}
当我调试通知扩展时,打印确实发生了。打印出来
"This is happening. ActionIdentifier: snoozeActionIdentifier"
。然而,通知不会被驳回。
如果我从
到
.不要解雇
.dismissAndForwardAction
,它会关闭并打开我的应用程序。
我找不到有这个问题的人。我想要
不予考虑
我现在看到这两种情况正在发生:
-
-
我在锁屏上用3D触摸我的通知,点击“Snooze”(什么都没发生),然后点击通知外,常规通知就会消失。