![](/img/trans.png)
[英]How to run application within UNNotificationContentExtension
[英]How to remove UNNotificationContentExtension displayed view
我使用UNNotificationContentExtension
從用戶那里進行調查。
條件是我沒有打開父應用程序。
這是表情符號動作
if #available(iOSApplicationExtension 12.0, *) {
// API call here
self.extensionContext?.dismissNotificationContentExtension()
} else {
// Fallback on earlier versions
}
每個表情符號都有動作。 當用戶點擊表情符號時,我會將響應發送到服務器並刪除此通知。 一切都將發生在擴展部分
有什么問題?
使用dismissNotificationContentExtension
通知立即關閉和隱藏。 它再次在通知屏幕中找到。 當用戶點擊表情符號按鈕時,如何刪除此通知。
這就是我的解決方案的工作方式。 缺點:刪除了所有已發送的相同類別的通知,而不是刪除當前消息。
@IBAction func btnActionHappy(_ sender: Any) {
UNUserNotificationCenter.current().getDeliveredNotifications { notifications in
if #available(iOSApplicationExtension 12.0, *) {
self.extensionContext?.dismissNotificationContentExtension()
} else {
// Fallback on earlier versions
}
let matchingNotifications = notifications.filter({ $0.request.content.categoryIdentifier == "debitOverdraftNotification" })
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: matchingNotifications.map({ $0.request.identifier }))
print("Somethings")
}
}
您可以使用 UNUserNotificationCenter 和 UNNotificationContentExtension 協議來完成
使用 UNUserNotificationCenter 添加操作
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization (options: [.alert, .sound]) {(_, _) in
}
let clearAction = UNNotificationAction(identifier: "sadEmoji", title: "Emoji", options: [])
let category = UNNotificationCategory(identifier: "NotifCategory", actions: [clearAction], intentIdentifiers: [], options: [])
center.setNotificationCategories([category])
在擴展視圖 controller 中添加協議 UNNotificationContentExtension 的委托方法
func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "sadEmoji" {
UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "NotifCategory")
}
completion(.dismiss)
}
試試看,讓我知道它有效。
您可以使用removeDeliveredNotifications(withIdentifiers:)
刪除當前通知。
var notification: UNNotification?
func didReceive(_ notification: UNNotification) {
self.notification = notification
...
}
@IBAction func btnActionHappy(_ sender: Any) {
if #available(iOSApplicationExtension 12.0, *) {
extensionContext?.dismissNotificationContentExtension()
}
if let identifier = notification?.request.identifier {
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [identifier])
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.