Ios 如何删除未通知的ContentExtension显示视图

Ios 如何删除未通知的ContentExtension显示视图,ios,objective-c,swift,push-notification,apple-push-notifications,Ios,Objective C,Swift,Push Notification,Apple Push Notifications,我使用UNNotificationContentExtension来调查用户 条件是我不打开父应用程序 下面是表情符号动作 if #available(iOSApplicationExtension 12.0, *) { // API call here self.extensionContext?.dismissNotificationContentExtension() } else { // Fallback on earl

我使用UNNotificationContentExtension来调查用户

条件是我不打开父应用程序

下面是表情符号动作

    if #available(iOSApplicationExtension 12.0, *) {

        // API call here
        self.extensionContext?.dismissNotificationContentExtension()
    } else {
        // Fallback on earlier versions
    }
每个表情符号都有动作。当用户点击表情符号时,我会将响应发送到服务器并删除此通知。一切都会发生在延伸部分

有什么问题吗


使用dismissNotificationContentExtension通知Dismission和hide instant。它再次出现在通知屏幕中。当用户点击表情符号按钮时,如何删除此通知。

您可以使用UnuseNotificationCenter和UNNotificationContentExtension协议删除此通知

使用UnuseNotificationCenter添加操作

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])
在扩展的视图控制器中添加协议UNNotificationContentExtension的委托方法

 func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
    if response.actionIdentifier == "sadEmoji" {
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "NotifCategory")
    }
    completion(.dismiss)
}

试试看,让我知道它是有效的。

您可以使用UnuseNotificationCenter和UNNotificationContentExtension协议来完成

使用UnuseNotificationCenter添加操作

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])
在扩展的视图控制器中添加协议UNNotificationContentExtension的委托方法

 func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void) {
    if response.actionIdentifier == "sadEmoji" {
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: "NotifCategory")
    }
    completion(.dismiss)
}

试试看,让我知道它的工作原理。

这就是我的解决方案的工作原理。 缺点:删除同一类别的所有已传递通知,而不是删除当前邮件

@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")

        }
}

这就是我的解决方案的工作原理。 缺点:删除同一类别的所有已传递通知,而不是删除当前邮件

@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")

        }
}

您可以使用removeDeliveredNotificationswithIdentifiers:删除当前通知


您可以使用removeDeliveredNotificationswithIdentifiers:删除当前通知


您指的是哪一个通知屏幕?您指的是哪一个通知屏幕?我将把UnuseNotificationCenterDelegate的代码放在哪里?让中心=未使用通知中心。当前中心。代理=自身?下面是示例代码。请参考,谢谢。我会检查的,现在可以工作了。然而,表情符号将作为一个行动表,而不是像我上传的用户界面。你觉得这个问题怎么样?谢谢你的热情支持。你的帮助让我明白这一点。我将把那些非通知中心的代码放在哪里?让中心=未使用通知中心。当前中心。代理=自身?下面是示例代码。请参考,谢谢。我会检查的,现在可以工作了。然而,表情符号将作为一个行动表,而不是像我上传的用户界面。你觉得这个问题怎么样?谢谢你的热情支持。你的帮助让我明白这一点。