Register and Schedule Local Notification in Swift 3.0 iOS 10
suggest changeRegistration
in AppDelegate
import UserNotifications
in didFinishLaunchingWithOptions method,
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge]) { (granted, error) in
// Here you can check Request is Granted or not.
}
Create and Schedule notification.
let content = UNMutableNotificationContent()
content.title = "10 Second Notification Demo"
content.subtitle = "From Wolverine"
content.body = "Notification after 10 seconds - Your pizza is Ready!!"
content.categoryIdentifier = "myNotificationCategory"
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 10.0,
repeats: false)
let request = UNNotificationRequest(
identifier: "10.second.message",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents