使用 ios 使用 swift 的本地通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37324331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:07:01  来源:igfitidea点击:

Local Notification using ios using swift

iosswiftlocalnotification

提问by Nipul Daki

I'm new in swift, I don't know how to implement local notification I have tried some code but it's not exactly work, so Anyone can help to implement local Notification in iOSusing swift?

我是 swift 的新手,我不知道如何实现本地通知我已经尝试了一些代码,但它并不完全有效,所以任何人都可以帮助在iOS使用中实现本地通知swift

回答by Ketan Parmar

Here i am sharing example,

我在这里分享例子,

Register for local notification,

注册本地通知,

@IBAction func registerLocal(sender: AnyObject) {
    let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
}

Schedule local notification,

安排本地通知,

@IBAction func scheduleLocal(sender: AnyObject) {
    let notification = UILocalNotification()
    notification.fireDate = NSDate(timeIntervalSinceNow: 5)
    notification.alertBody = "Hey you! Yeah you! Swipe to unlock!"
    notification.alertAction = "be awesome!"
    notification.soundName = UILocalNotificationDefaultSoundName
    notification.userInfo = ["CustomField1": "w00t"]
    UIApplication.sharedApplication().scheduleLocalNotification(notification)


    guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() else { return }

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

}

It will fire local notification after 5 seconds.

之后会触发本地通知5 seconds

Hope this will help :)

希望这会有所帮助:)

回答by user1195300

There are lots of tutorials online for this, you could just Google it.

网上有很多教程,你可以谷歌一下。

Here's a tutorial: http://jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

这是一个教程:http: //jamesonquave.com/blog/local-notifications-in-ios-8-with-swift-part-1/

Also here's an example of a local notification:

这里还有一个本地通知的例子:

let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Alert!"
notification.alertAction = "open"
notification.hasAction = true
notification.userInfo = ["UUID": "reminderID" ]
UIApplication.sharedApplication().scheduleLocalNotification(notification)