【问题标题】:How to remove UNNotificationContentExtension displayed view如何删除 UNNotificationContentExtension 显示的视图
【发布时间】:2020-07-11 08:26:59
【问题描述】:

我使用UNNotificationContentExtension 向用户进行调查。

条件是我没有打开父应用。

这是表情符号动作

    if #available(iOSApplicationExtension 12.0, *) {

        // API call here
        self.extensionContext?.dismissNotificationContentExtension()
    } else {
        // Fallback on earlier versions
    }

每个表情符号都有动作。当用户点击表情符号时,我会将响应发送到服务器并删除此通知。一切都会发生在扩展部分

有什么问题?

使用dismissNotificationContentExtension 通知立即关闭和隐藏。它再次在通知屏幕中找到。当用户点击表情符号按钮时,如何删除此通知。

【问题讨论】:

  • 您指的是哪个通知屏幕?

标签: ios objective-c swift push-notification apple-push-notifications


【解决方案1】:

这就是我的解决方案的工作方式。 缺点:删除了所有已发送的相同类别的通知,而不是删除当前消息。

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

        }
}

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

    • 我将把那些 UNUserNotificationCenterDelegate 的代码放在哪里?让 center = UNUserNotificationCenter.current() center.delegate = self ........ ?
    • 非常感谢。我会检查的。
    • 通知删除正在工作。但是,表情符号将作为操作表出现,不像我上传的 UI。你觉得这个问题怎么样?
    • 感谢您的热情支持。你的帮助让我弄清楚这一点。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多