Yar*_*nST 6 notifications ios swift notification-content-extension
是否可以通过 NotificationContentExtension 中的按钮解除/取消本地通知?
我只能解除 NotificationContentExtension 本身,但不能解除整个通知。
if #available(iOSApplicationExtension 12.0, *) {
self.extensionContext?.dismissNotificationContentExtension()
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 UNUserNotificationCenter 和 UNNotificationContentExtension 协议来做到这一点
使用 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])
Run Code Online (Sandbox Code Playgroud)
在扩展的视图控制器中添加协议 UNNotificationContentExtension 的委托方法
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "ClearNotif" {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().removeAllDeliveredNotifications()
}
completion(.dismiss)
}
Run Code Online (Sandbox Code Playgroud)
试试看,让我知道它有效。
| 归档时间: |
|
| 查看次数: |
239 次 |
| 最近记录: |