User notification is a new local notification feature in iOS. It gives the additional functionality i.e. to remove pending notifications which is not possible in UILocalNotification. So lets start how it works.
As we know for scheduling notification we want one date and time so we will place a date picker in default view controller.
Now in App Delegate Class do below settings->
1. import UserNotifications -> import user notification in app delegate class.
2. Now make one method named: "scheduleUserNotification" and do below coding. In this method we will get date from date picker in view controlller.
func scheduleUserNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date) // this will break the date into components
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false) // we want notification to be displayed once, UNCalendarNotificationTrigger requires date components, not dates, to function.
// below code is used to display the content of notifications.
let content = UNMutableNotificationContent()
content.title = "Content of notification"
content.body = "Hello this is my new UNNotification"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
// below code is to used remove all pending notifications, so that we wont get any unnecessary duplicate notifications.
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
3. Now add below code in didFinishLaunchingWithOptions method. This is used to ask user permission for notifications.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}
4. In view controller class make an IBAction named: "DatePickerAction" of date picker where we select date and time for notification and write below code.
@IBAction func DatePickerAction(_ sender: UIDatePicker) {
// in this method we are selecting date and time of notification
let selectedDate = sender.date
let delegate = UIApplication.shared.delegate as? AppDelegate
delegate?.scheduleUserNotification(at: selectedDate) // calling app delegate method
}
5. Now run the app. You will see the authorization request. Make sure you accept it otherwise you wont receive notifications.
The Next important thing is notification will not get displayed in an app automatical or we can say by default. For that, After setting the date form the date picker, you have to make sure to go back to the home screen or lock screen and after then you are able to see notification which you scheduled.
0 Comment(s)