Local notifications allow your app to notify the user about content which does not require the use of a server.
Unlike remote notifications which are triggered from a server, local notifications are scheduled and triggered within an app. Notifications in general are targeted to increase user interaction with the app, inviting or tempting the user to open and interact with it.
UILocalNotification was deprecated in iOS 10. Use the UserNotifications framework instead.
Do not confuse UILocalNotification with push notifications. UILocalNotification is triggered by your device, and when scheduled, is copied to the system.
To see the notification in the iOS Simulator, type ^⌘H (control-command-H) to go home and then type ⌘L (command-L) to lock the device. Wait a few seconds, and the notification should appear (this appearance will vary depending on notification type discussed in "Registering for local notifications"):
Swipe on the notification to get back to the app (note that if you called this in the first view controller's viewDidLoad, viewWillAppear, viewDidAppear, etc., the notification will be scheduled again).
Managing local notifications using UUID
Often times you will need to be able to manage your notifications, by being able to keep track of them and cancel them.
Track a notification
You can assign a UUID (universally unique identifier) to a notification, so you can track it:
Cancel a notification
To cancel a notification, we first get a list of all the notifications and then find the one with a matching UUID. Finally, we cancel it.
You would probably want to store all of these UUID's in Core Data or Realm.
Custom sounds may be provided for notifications generated by your app. When the system displays an alert for a local notification or badges an app icon, it plays this sound (so long as the user has not disabled notification sounds).
The default value is nil which means no sound is played for your notification.
To supply a custom sound, add a .caf, .wav, or .aiff file to your app bundle. Sounds that last longer than 30 seconds are not supported. Supplying a sound that does not meet those requirements will cause the default sound to play (UILocalNotificationDefaultSoundName).
Presenting a local notification immediately
If you want to show local notification immediately, you should call:
An advantage of using this is so you won't have to set the fireDate and timeZone properties of your UILocalNotification object.
Register and Schedule Local Notification in Swift 3.0 (iOS 10)
in didFinishLaunchingWithOptions method,
Create and Schedule notification.
Where ever this part of code is triggered, If you have allowed Notification Permission, you will receive an notification.
To test it properly, Make sure your application in Background mode.
Registering for local notifications
In order to present local notifications to the user, you have to register your app with the device:
This will present an alert the first time it is called:
Regardless of what the user chooses, the alert will not show up again and changes will have to be initiated by the user in Settings.
Responding to received local notification
IMPORTANT: This delegate method is only called in the foreground.
This method is generally overridden in the AppDelegate, which conforms to the UIApplicationDelegate protocol.
what's new in UILocalNotification with iOS10
You can use UILocalNotification, old APIs also works fine with iOS10, but we had better use the APIs in the User Notifications framework instead. There are also some new features, you can only use with iOS10 User Notifications framework.
This also happens to Remote Notification, for more information: Here.
Now you can either present alert, sound or increase badge while the app is in foreground too with iOS 10
Now you can handle all event in one place when user tapped (or slided) the action button, even while the app has already been killed.
Support 3D touch instead of sliding gesture.
Now you can remove specifical local notification just by one row code.
Support Rich Notification with custom UI.
It is really easy for us to convert UILocalNotification APIs to iOS10
User Notifications framework APIs, they are really similar.
I write a Demo here to show how to use new and old APIs at the same time: iOS10AdaptationTips .