ios – 用户通知取消,swift3

我正在使用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)
}

相关文章

在有效期内的苹果开发者账号(类型为个人或者公司账号)。还...
Appuploader官网--IOS ipa上传发布工具,证书制作工具跨平台...
苹果在9月13号凌晨(北京时间)发布 iOS 16,该系统的设备可...
计算机图形学--OpenGL递归实现光线追踪
Xcode 14打出来的包在低版本系统运行时会崩溃,报错信息是Li...