Local Notification using ios using swift [closed]

房东的猫 提交于 2019-11-29 14:08:14

问题


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 iOS using swift?


回答1:


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.

Hope this will help :)




回答2:


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/

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)


来源:https://stackoverflow.com/questions/37324331/local-notification-using-ios-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!