我正在使用app,用户可以为每日本地通知设置时间,然后我可以选择启用或禁用这些通知.我的问题如何禁用/取消已设置的通知?
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 }
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") } })
谢谢.
解决方法
要取消所有待处理通知,您可以使用以下命令:
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
要取消特定通知,
UNUserNotificationCenter.current().getPendingNotificationRequests { (notificationRequests) in var identifiers: [String] = [] for notification:UNNotificationRequest in notificationRequests { if notification.identifier == "identifierCancel" { identifiers.append(notification.identifier) } } UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: identifiers) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。