• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 迁移到 iOS10 时无法调用 UNNotificationCategory 类型的初始化程序

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:42:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在使用一个示例项目来处理新通知,从 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: _)'



Best Answer-推荐答案


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

我在工作中使用的是 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap