[英]Dismiss IOS Notification from NotificationContentExtention
Is it possible to dismiss/cancel a local notification from a button with in NotificationContentExtension?是否可以通过 NotificationContentExtension 中的按钮关闭/取消本地通知?
I was only able to dismiss the NotificationContentExtension itself, but not the entire notification.我只能关闭 NotificationContentExtension 本身,但不能关闭整个通知。
if #available(iOSApplicationExtension 12.0, *) {
self.extensionContext?.dismissNotificationContentExtension()
}
You can do it using UNUserNotificationCenter & UNNotificationContentExtension protocol您可以使用 UNUserNotificationCenter 和 UNNotificationContentExtension 协议来完成
Add action using UNUserNotificationCenter使用 UNUserNotificationCenter 添加操作
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization (options: [.alert, .sound]) {(_, _) in
}
let clearAction = UNNotificationAction(identifier: "ClearNotif", title: "Clear", options: [])
let category = UNNotificationCategory(identifier: "ClearNotifCategory", actions: [clearAction], intentIdentifiers: [], options: [])
center.setNotificationCategories([category])
Add a delegate method of the protocol UNNotificationContentExtension in your extension's view controller在扩展视图 controller 中添加协议 UNNotificationContentExtension 的委托方法
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "ClearNotif" {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
completion(.dismiss)
}
Try it and let me know it works.试试看,让我知道它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.