ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始值设定项

标签 ios notifications swift2 ios10

我正在使用一个示例项目来处理新通知,从 Swift 2.2 过渡到 Swift 2.3,也从 iOS 9 过渡到 iOS 10。我使用下面的代码,取自 this example :

@available(iOS 10.0, *)
func setupAndGenerateLocalNotification() {
    // Register an Actionable Notification
    let highFiveAction = UNNotificationAction(identifier: NotificationActions.HighFive.rawValue, title: "High Five", options: [])

    //next line is an error:
    let category = UNNotificationCategory(identifier: "wassup", actions: [highFiveAction], minimalActions: [highFiveAction], intentIdentifiers: [], options: [.customDismissAction])
    //Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

    UNUserNotificationCenter.current().setNotificationCategories([category])

    let highFiveContent = UNMutableNotificationContent()
    highFiveContent.title = "Wassup?"
    highFiveContent.body = "Can I get a high five?"

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

    let highFiveRequestIdentifier = "sampleRequest"
    let highFiveRequest = UNNotificationRequest(identifier: highFiveRequestIdentifier, content: highFiveContent, trigger: trigger)
    UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(highFiveRequest) { (error) in
        // handle the error if needed
        print(error)
    }
}

我得到的错误是

Cannot invoke initializer for type 'UNNotificationCategory' with an argument list of type '(identifier: String, actions: _, minimalActions: _, intentIdentifiers: _, options: _)'

最佳答案

您没有提到您的错误是什么,但我怀疑您复制的代码是为比您安装的更早的测试版编写的。

我在工作中使用 Swift 3,而不是 Swift 2.3。从 Xcode 8 beta 5 开始,您应该将 UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest 更改为 UNUserNotificationCenter.current().add,并删除 minimalActions: [highFiveAction], 来自您的 UNNotificationCategory。有可能只有后者才适用于 Swift 2.3?

我希望这能让您继续前进,但请注意:在 iOS 10 最终版之前,这可能会再次发生变化。特别是,我怀疑 current() 可能会变成 current

关于ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849693/

相关文章:

swift - String 的 `endIndex` 和 `count` 不同

ios - 数字和名称的复数化(swift stringsdict)

ios - 在 iOS 中拍摄没有相机 View 的照片

ios - 尝试与 Opengl 纹理共享 Metal 纹理

ios - 我可以在任何 Apple 计算机上安装我作为开发人员签名的 Mac OS X 应用程序吗?

iphone - 本地通知和 Storyboard

java - 开源 Java EE 通知框架

javascript - 如何使用 laravel 每 x 秒自动刷新一个 div

swift 2.0 : Schedule background task to upload failed product uploads

ios - 在数组上使用 NSUserDefaults