Skip to content

Instantly share code, notes, and snippets.

@leonmak
Last active August 10, 2017 21:30
Show Gist options
  • Save leonmak/887e484852dc1dc161430a00ff82e036 to your computer and use it in GitHub Desktop.
Save leonmak/887e484852dc1dc161430a00ff82e036 to your computer and use it in GitHub Desktop.
Local Notifications - iOS Swift 3

Local notifications

  1. ViewController
import UserNotifications
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow, error in })
    }

    @IBAction func pushBtn(_ sender: Any) {
        let content = UNMutableNotificationContent()
        content.title = "How many days are there in one year"
        content.subtitle = "Do you know?"
        content.body = "Do you really know?"
        content.badge = 1
        
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
        let request = UNNotificationRequest(identifier: "timerDone", content: content, trigger: trigger)
        
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment