【问题标题】:Dismiss IOS Notification from NotificationContentExtention从 NotificationContentExtention 关闭 IOS 通知
【发布时间】:2020-06-25 22:27:26
【问题描述】:

是否可以通过 NotificationContentExtension 中的按钮关闭/取消本地通知?

我只能关闭 NotificationContentExtension 本身,而不是整个通知。

if #available(iOSApplicationExtension 12.0, *) {
          self.extensionContext?.dismissNotificationContentExtension()
}

【问题讨论】:

  • 关闭NotificationContentExtension关闭整个通知有什么区别?
  • 关闭通知会将其从锁定屏幕中完全删除。关闭内容扩展会撤消长按效果(显示按钮或自定义 GUI)

标签: ios swift notifications notification-content-extension


【解决方案1】:

您可以使用 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])

在扩展的视图控制器中添加协议 UNNotificationContentExtension 的委托方法

 func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
    if response.actionIdentifier == "ClearNotif" {
        UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
        UNUserNotificationCenter.current().removeAllDeliveredNotifications()
    }
    completion(.dismiss)
}

试试吧,让我知道它有效。

【讨论】:

  • 不完全符合我的要求,但它为我指明了方向。谢谢
  • @YardenST 你在找什么?你不希望它成为一个动作?你想让它成为一个普通的按钮吗?
  • 我希望它是 ContentExtension 中的一个按钮,我只想删除那个通知。通知中心并非所有通知
  • @YardenST 您可以使用 UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ) 删除单个通知。
  • @YardenST,我无法关闭扩展通知。即使使用 UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: )。删除扩展通知的任何其他想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
相关资源
最近更新 更多