【问题标题】:Handle notification dismiss action when app is on the background当应用程序在后台时处理通知关闭操作
【发布时间】:2022-01-27 13:44:51
【问题描述】:
我正在尝试听取用户清除推送通知操作。我有下面的代码,当应用程序在前台时它正在工作,但是当我将应用程序放在后台时它不会进入该功能。当用户清除后台状态的通知时,有什么方法可以进行一些编码?
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if(response.actionIdentifier == UNNotificationDismissActionIdentifier){
...
//I need to do some coding here!
}
}
【问题讨论】:
标签:
swift
push-notification
background
dismiss
【解决方案1】:
我用它在后台接收远程通知:
extension AppDelegate: UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Process this userInfo dictionary
processNotification(dictionary: userInfo)
}
在 AppDelegate 的 willFinishLaunchingWithOptions 中,我称之为:
fileprivate func setupAPN(application: UIApplication) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
// For iOS 10 data message (sent via FCM)
MobilePlatformPush.setRemoteDelegate(delegate: self)
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
application.registerForRemoteNotifications()
}