ios - 用户通知请求始终带有默认操作标识符

原文 标签 ios swift ios10 unusernotificationcenter

我正在使用 UNUserNotificationCenterDelegate (> ios 10) 和一种委托(delegate)方法,我可以在其中检查来自通知的响应,无论我做什么,actionIdentifier 总是等于“com.apple.UNNotificationDefaultActionIdentifier”。 “response.notification.request.content.categoryIdentifier”是正确的,具有预期的值,但 request.actionIdentifier 永远不会正确(下面示例中的“mycustomactionidentifier”)。有谁知道我是否遗漏了什么?

extension NotificationManager: UNUserNotificationCenterDelegate {


    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) {

        completionHandler([.alert,.sound])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Swift.Void) {

        if response.notification.request.content.categoryIdentifier == "TEST" {
            if response.actionIdentifier == "mycustomactionidentifier" {
                NSLog("it finally works dude!")
            }
        }

        completionHandler()
    }
}

我将操作和类别添加到通知中心:
    let uploadAction = UNNotificationAction(identifier: "mycustomactionidentifier", title: "Uploaded", options: [])
    let category = UNNotificationCategory(identifier: "TEST", actions: [uploadAction], intentIdentifiers: [])
    center.setNotificationCategories([category])

并且正在发送包含正确标识符的请求:
    let uploadContent = UNMutableNotificationContent()
    uploadContent.title = String(number) + " asset(s) added"
    uploadContent.body = "Check your inventory to manage your assets!"
    uploadContent.categoryIdentifier = "TEST" 

    let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 6, repeats: false)

    let uploadRequestIdentifier = "mycustomactionidentifier"
    let uploadRequest = UNNotificationRequest(identifier: uploadRequestIdentifier, content: uploadContent, trigger: trigger)
    UNUserNotificationCenter.current().add(uploadRequest, withCompletionHandler: nil)

最佳答案

首先:注册您的自定义操作:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.

    UNUserNotificationCenter.current().delegate = self
    UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
        if granted {
         // Access granted
        } else {
         // Access denied
        }
    }

    self.registerNotificationAction()

    return true
}

func registerNotificationAction() {

    let first = UNNotificationAction.init(identifier: "first", title: "Action", options: [])
    let category = UNNotificationCategory.init(identifier: "categoryIdentifier", actions: [first], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])
}

并创建一个具有唯一标识符的内容:
func scheduleNotification() {

    // Create a content
    let content = UNMutableNotificationContent.init()
    content.title = NSString.localizedUserNotificationString(forKey: "Some title", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "Body of notification", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "categoryIdentifier"

    // Create a unique identifier for each notification
    let identifier = UUID.init().uuidString

    // Notification trigger
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)

    // Notification request
    let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

    // Add request
    UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)

}

最后:使用默认和自定义操作处理通知。
   extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        if response.notification.request.content.categoryIdentifier == "categoryIdentifier" {

            switch response.actionIdentifier {
            case UNNotificationDefaultActionIdentifier:
                print(response.actionIdentifier)
                completionHandler()
            case "first":
                print(response.actionIdentifier)
                completionHandler()
            default:
                break;
            }
        }
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

        completionHandler([.alert, .sound])
    }
}

希望能帮助到你!

第二版

结果如下: 这将是我们的 UNNotificationDefaultActionIdentifier :

UNNotificationDefaultActionIdentifier

这是通知的扩展版本,我们可以处理两种操作:

Expanded notification

关于ios - 用户通知请求始终带有默认操作标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43614793/

相关文章:

ios - 如何使用PHImageManager获取原始图像

ios - becomeFirstResponder 在 ios 10 上有时不会打开键盘

xcode - GIDSignIn 钥匙串(keychain)错误 iOS 10 Xcode 8

ios - 我可以在iOS应用程序和相应的Facebook游戏之间共享相同的App ID和App Secret吗?

ios - 调用“reverse()”的结果未使用

swift - 通过共享表从NSData共享PDF

swift : OverlayView that appears at the bottom of the screen

swift - 快速获取具有存储在另一个变量中的名称的类属性

ios - iOS 8 和 iOS 9 上的 NSURLSession 之间的区别?

ios - iOS应用右移,无法查看完整表格