使用swos使用ios的本地通知

Nip*_*aki 8 ios localnotification swift

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

Lio*_*ion 35

我在这里分享一个例子,

注册本地通知,

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

安排本地通知,

@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
    }

}
Run Code Online (Sandbox Code Playgroud)

之后会发出本地通知5 seconds.

希望这会有所帮助:)


use*_*300 4

网上有很多这方面的教程,你可以google一下。

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

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

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