【问题标题】:User Notification request always come with default action identifier用户通知请求始终带有默认操作标识符
【发布时间】:2017-09-22 17:04:12
【问题描述】:

我正在使用 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)

【问题讨论】:

  • 当通知出现在您的屏幕上时,它是什么样的?可以加个截图吗?你点击哪个动作?
  • 您的比较有误。您的操作标识符是“mycustomactionidentifier”,但您已检查为“mycustomidentifier”。因此编译器只是忽略了你的动作标识符。
  • 嗨@Honey,这是一个常规通知,标题和正文是我选择的。无论我在哪里单击通知,它都会返回正确的 categoryIdentifier 但错误的 actionIdentifier!
  • 和@Mannopson,对不起,这是一个错字,但在我的代码中它是正确的,但仍然无法正常工作。谢谢!

标签: ios swift ios10 unusernotificationcenter


【解决方案1】:

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

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

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

【讨论】:

  • 嗨 Mannopson,我完全按照你说的做,但是当我发送带有操作标识符“X”的通知时,当我单击通知时,我得到一个“com.apple.UNNotificationDefaultActionIdentifier”而不是“X”在 didReceive 响应函数中。感谢您的回答。
  • @user2116499 何时向用户发送通知?用户点击它,就会调用 UNNotificationDefaultActionIdentifier。因为它是默认动作,是系统提供的。我的建议是完美的。
  • 嗨@Mannopson,您创建了一个标识符为“first”的操作。当您单击通知时,它不会出现action identifier =“first”吗?我看到教程说这应该是行为。如果不是,那么你的答案是正确的。谢谢!
  • @user2116499 这是不可能的!当通知显示在顶部时,将其拉下,然后单击“操作”按钮。
  • 很好!对不起,@Mannopson。非常感谢您的支持/帮助。
【解决方案2】:

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

response.notification.request.identifier

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    相关资源
    最近更新 更多