ios – 用户通知取消,swift3
发布时间:2020-12-15 01:43:14 所属栏目:百科 来源:网络整理
导读:我正在使用app,用户可以为每日本地通知设置时间,然后我可以选择启用或禁用这些通知.我的问题如何禁用/取消已设置的通知? 在其中一个IF我需要取消通知 override func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) - CGFloat {
我正在使用app,用户可以为每日本地通知设置时间,然后我可以选择启用或禁用这些通知.我的问题如何禁用/取消已设置的通知?
在其中一个IF我需要取消通知 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) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |