简体   繁体   English

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

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

I had my older code written in swift 2.2 for registering and triggering UILocalNotification . 我用UILocalNotification 2.2编写了较早的代码,用于注册和触发UILocalNotification Now when I migrated from Swift 2.2 to 2.3 in xcode 8 and IOS 9 to IOS 10. I am not able to receive any local notification after that. 现在,当我从xcode 8中的Swift 2.2迁移到2.3,以及从IOS 9迁移到IOS 10时,我在此之后无法接收任何本地通知。 Also, not getting any exception in console though. 另外,虽然在控制台中没有任何异常。 Can confirm it was working fine earlier with IOS 9. 可以确认它在IOS 9上运行良好。

UIApplication.sharedApplication().scheduleLocalNotification(notification)

This is the code I am using to trigger my local notification. 这是我用来触发本地通知的代码。

I can see the doc saying this method has been deprecated in IOS 10 and need to use UNUserNotificationCenter instead. 我可以看到文档说此方法已在IOS 10中弃用,而需要使用UNUserNotificationCenter But to use that I need to upgrade to Swift 3.0. 但是要使用它,我需要升级到Swift 3.0。

Hence, my question is, is it a bug/loophole in Swift 2.3 ? 因此,我的问题是,这是Swift 2.3中的错误/漏洞吗? Do we have any work around for that ? 为此,我们有什么解决方法吗? Or am I missing anything ? 还是我错过了什么?

This is the Swift 2.3 code I use to show local notifications on my app for iOS 10 users: 这是我用来在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)


    }}

I believe 2.3 gives you access to the new API. 我相信2.3可让您访问新API。 You should be able to just use the new API UNUserNotificationCenter 您应该可以只使用新的API UNUserNotificationCenter

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

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