使用ios使用swift的本地通知
我是新的迅速,我不知道如何实现本地通知我已经尝试了一些代码,但它不完全工作,所以任何人都可以帮助在iOS
使用swift
实现本地通知?
在这里,我分享的例子,
注册本地通知,
@IBAction func registerLocal(sender: AnyObject) { let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) }
安排本地通知,
@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 } }
它会在5 seconds
后触发本地通知。
有很多教程也可以像Appcoda一样! 只要谷歌,你会得到很多
希望这会帮助:)
网上有很多的教程,你可以直接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)