【问题标题】:Local notification is not invoking in IOS 10 + Swift 2.3IOS 10 + Swift 2.3 中未调用本地通知
【发布时间】:2016-10-21 04:22:38
【问题描述】:

我的旧代码是用 swift 2.2 编写的,用于注册和触发 UILocalNotification。现在,当我在 xcode 8 中从 Swift 2.2 迁移到 2.3 并从 IOS 9 迁移到 IOS 10 时。之后我无法收到任何本地通知。此外,虽然控制台中没有任何异常。可以确认它之前在 IOS 9 上运行良好。

UIApplication.sharedApplication().scheduleLocalNotification(notification)

这是我用来触发本地通知的代码。

我可以看到文档说此方法在 IOS 10 中已被弃用,需要改用 UNUserNotificationCenter。但要使用它,我需要升级到 Swift 3.0。

因此,我的问题是,它是 Swift 2.3 中的错误/漏洞吗?我们有什么解决办法吗?还是我错过了什么?

【问题讨论】:

  • UILocalNotifications 在 iOS 10 上已被弃用,但并未被移除,因此您仍然可以使用
  • 我正在使用它,但即使它已安排,也无法收到任何通知。在 ios 9 中运行正常。
  • "需要改用 UNUserNotificationCenter。但要使用它,我需要升级到 Swift 3.0" 不,这完全是假的。 语言系统无关。

标签: ios uilocalnotification ios10 swift2.3 unusernotificationcenter


【解决方案1】:

这是我用来在我的应用上为 iOS 10 用户显示本地通知的 Swift 2.3 代码:

func showLocalNotif() {      


    if #available(iOS 10, *) {
        let content = UNMutableNotificationContent()
        content.title = "your title"
        content.subtitle = "your subtitle"
        content.body = "your message to the users"
        content.categoryIdentifier = "message"

        //Set the trigger of the notification -- here a timer.
        let trig = UNTimeIntervalNotificationTrigger(
            timeInterval: 100,
            repeats: false)

        //Set the request for the notification from the above
        let request = UNNotificationRequest(
            identifier: "100.second.message",
            content: content,
            trigger: trig

        )

        //Add the notification to the current notification center
        UNUserNotificationCenter.currentNotificationCenter().addNotificationRequest(request, withCompletionHandler: addNotificationRequestHandler)


    }}

【讨论】:

    【解决方案2】:

    我相信 2.3 可以让您访问新的 API。您应该可以只使用新的 API UNUserNotificationCenter

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 2017-02-12
      • 1970-01-01
      相关资源
      最近更新 更多