Notificações de usuário cancel, swift3
Estou trabalhando no aplicativo em que o usuário pode definir o horário da notificação local todos os dias e, em seguida, tenho a opção de ativar ou desativar essas notificações. Minha pergunta: como posso desativar / cancelar a notificação já configurada?
Aqui em um desses FIs eu preciso cancelar o notif
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (indexPath as NSIndexPath).row == 1 && switchiBtn.isOn == false {
return 0.0
}
if (indexPath as NSIndexPath).row == 2 {
if switchiBtn.isOn == false || dpVisible == true {
return 0.0
}
return 216.0
}
if (indexPath as NSIndexPath).row == 3 {
if switchiBtn.isOn == false || dpVisible == true {
return 0.0
}
return 44
}
return 50.0
}
Aqui estão as funções para agendamento e botão em que estou configurando o notif.
func scheduleNotif(date: DateComponents, completion: @escaping (_ Success: Bool) -> ()) {
let notif = UNMutableNotificationContent()
notif.title = "Your quote for today is ready."
notif.body = "Click here to open an app."
let dateTrigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: { error in
if error != nil {
print(error)
completion(false)
} else {
completion(true)
}
})
}
@IBAction func setACTION(_ sender: AnyObject) {
let hour = datePicker.calendar.component(.hour, from: datePicker.date)
let minute = datePicker.calendar.component(.minute, from: datePicker.date)
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
print(dateComponents)
scheduleNotif(date: dateComponents, completion: { success in
if success {
print("SUCCESS")
} else {
print("ERROR")
}
})
Obrigado.