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
?
Asked
Active
Viewed 3.9k times
10

Ketan Parmar
- 27,092
- 9
- 50
- 75

Nipul Daki
- 371
- 1
- 3
- 17
-
3There are a lot of tutorials on google for this. Event Apple documentation can explain you how it works. – David Ansermot May 19 '16 at 13:21
-
Take a look at [this Swift 3 example](https://stackoverflow.com/a/45247943/4754881) for local notifications with [UNUserNotificationCenter](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter). – Derek Soike Jul 21 '17 at 23:12
2 Answers
36
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 :)

ymulki
- 432
- 2
- 15

Ketan Parmar
- 27,092
- 9
- 50
- 75
-
`UILocalNotification` was deprecated. Would suggest using this: https://developer.apple.com/documentation/usernotifications/unmutablenotificationcontent – Aviv Ben Shabat Jan 17 '19 at 08:05
-
'UILocalNotification' was deprecated in iOS 10.0: Use UserNotifications Framework's UNNotificationRequest – Naveed Ahmad Aug 02 '19 at 06:53
-
It is way easier to use my third party wrapper for Local notification in iOS. Just with 3 lines of code. https://github.com/Salarsoleimani/SwiftLocalNotification – Salar Soleimani Jun 24 '20 at 19:23
4
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)

Oli
- 117
- 1
- 10
-
'UILocalNotification' was deprecated in iOS 10.0: Use UserNotifications Framework's UNNotificationRequest – Naveed Ahmad Aug 02 '19 at 06:53