用户通知请求始终带有默认操作标识符 [英] User Notification request always come with default action identifier

查看:108
本文介绍了用户通知请求始终带有默认操作标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the UNUserNotificationCenterDelegate (> ios 10) and one of the delegate methods where I can check the response from the notification has always actionIdentifier equal "com.apple.UNNotificationDefaultActionIdentifier" no matter what I do. The "response.notification.request.content.categoryIdentifier" comes right, with the expected value, but the request.actionIdentifier never comes correctly ("mycustomactionidentifier" in the example below). Does anyone know if I'm missing something?

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

我添加了动作和通知中心的类别:

I added the action and category to the Notification center:

    let uploadAction = UNNotificationAction(identifier: "mycustomactionidentifier", title: "Uploaded", options: [])
    let category = UNNotificationCategory(identifier: "TEST", actions: [uploadAction], intentIdentifiers: [])
    center.setNotificationCategories([category])

并发送请求并输入正确的标识符:

and am sending the request putting the correct identifier:

    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)


推荐答案

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

Firstly: Register your custom actions:

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

并创建具有唯一标识符的内容:

And create a content with an unique identifier:

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)

}

最后:处理通知的默认值和自定义操作。

Lastly: Handle the notification with their default and custom actions.

   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

Here's the results: This is going to be our UNNotificationDefaultActionIdentifier:

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

And this one is expanded version of the notification, we could handle both actions:

这篇关于用户通知请求始终带有默认操作标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆