On this example we will discuss about using trigger with calendar.
We have discussed about scheduling a local notification using time interval and we understand that notification by default only show when the app is not currently on front. We use delegate method to make in-app notification.
We use calendar as a trigger to notify on specific date and time. Precisely we use DateComponent object, we can create triggers that include only the components that being setted. This behaviour makes it easier to create repeating triggers.
For example we want to notify every morning at 8:30 am, we only set the hour and minute components only, and set trigger to repeat. It will repeatly notify whenever any date that have hour and minutes that match with the components.
var dateCompo = DateComponents()
dateCompo.hour = 8
dateCompo.minute = 30
let trigger = UNCalendarNotificationTrigger.init(dateMatching: date, repeats: true)
We will go step by step how to use UserNotification framework.
Add inside viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])
{ (granted, error) in
// Enable or disable features based on authorization
}
let current = UNUserNotificationCenter.current()
current.delegate = self
Also add this conformation declaration UNUserNotificationCenterDelegate
.
Add this delegate’s implementation.
// MARK:- UNUserNotificationCenterDelegate
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// To show the banner in-app
completionHandler([.badge, .alert, .sound])
}
For testing out the function it’s advisable to use the nearest time so we can save time waiting the notification goes off.
// set this with your nearest current time
let hour = 6
let minute = 14
let content = UNMutableNotificationContent()
content.title = "Alarm"
content.body = "Alarm for \(hour):\(minute)"
content.sound = UNNotificationSound.default()
We will create the DateComponents object, and set the hour, and minute based on variables hour and minute. Then we create a trigger using UNCalendarNotificationTrigger
, initiate with date component and set repeats as true.
var dateCompo = DateComponents()
dateCompo.hour = hour
dateCompo.minute = minute
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateCompo, repeats: true)
let request = UNNotificationRequest(identifier: "alarm-id", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
The final code that being schedule inside viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge])
{ (granted, error) in
// Enable or disable features based on authorization
}
let current = UNUserNotificationCenter.current()
current.delegate = self
let hour = 6
let minute = 14
let content = UNMutableNotificationContent()
content.title = "Alarm"
content.body = "Alarm for \(hour):\(minute)"
content.sound = UNNotificationSound.default()
var dateCompo = DateComponents()
dateCompo.hour = hour
dateCompo.minute = minute
let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateCompo, repeats: true)
let request = UNNotificationRequest(identifier: "alarm-id", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request)
}
}
Now run the app, notification will get scheduled when view did load, to the specific hour and minute that being setted. It will be notified everyday at the same time.
Wait for tomorrow to see the notification goes off again.