how to set local notification in swift
let content = UNMutableNotificationContent()
content.title = "notification title"
content.categoryIdentifier = "identifier"
content.body = "notification body"
content.sound = UNNotificationSound.default
let triggerDateComponent = Calendar.current.dateComponents([.day, .hour, .minute], from: Date().addHours(5))
let trigger = UNCalendarNotificationTrigger(dateMatching: triggerDateComponent, repeats: false)
let request = UNNotificationRequest(identifier: "notification identifier", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
if error != nil {
print(error!)
} else {
print("\(request.description)")
}
})
how to get pending notification in swift
center.getPendingNotificationRequests { (notifications) in
print("Count: \(notifications.count)")
for item in notifications {
let id = item.identifier
let time = item.content
let categoryId = time.categoryIdentifier
print(time)
print("id: \(id)")
guard let trigger = item.trigger as? UNCalendarNotificationTrigger else { return }
var day = trigger.dateComponents.weekday
let nextFireDate = trigger.nextTriggerDate()
let title = item.content.body
}
}
how to remove notification in swift
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: ["notification identifire"])