繁体   English   中英

在iOS 10 + Swift 2.3中未调用本地通知

[英]Local notification is not invoking in IOS 10 + Swift 2.3

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

这是我用来在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.3可让您访问新API。 您应该可以只使用新的API UNUserNotificationCenter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM