1

我用 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 中的错误/漏洞吗?我们有什么解决办法吗?还是我错过了什么?

4

2 回答 2

0

这是我用来在我的应用程序上为 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)


    }}
于 2016-11-07T20:20:23.820 回答
-1

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

于 2016-10-29T03:55:53.703 回答