首页 文章

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

提问于
浏览
6

我正在使用UNUserNotificationCenterDelegate(> ios 10)和其中一个委托方法,我可以检查来自通知的响应总是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)

2 回答

  • 8

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

    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])
        }
    }
    

    希望能帮助到你!

    Second Edition

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

    UNNotificationDefaultActionIdentifier

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

    Expanded notification

  • 1

    正如Mannopson所说,您可以注册默认操作标识符 . 然而,我虽然你需要的是另一件事:

    response.notification.request.identifier

    来自Apple's actionIdentifier description表示此参数可能包含一个UNNotificationAction对象的标识符,或者它可能包含系统定义的标识符 . 这意味着你需要注册一个,希望我是对的(因为我是swift的新手)

相关问题