UserNotifications取消,swift3

den*_*cka 9 xcode ios nsusernotification swift

我正在使用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
}
Run Code Online (Sandbox Code Playgroud)

以下是日程安排和按钮的功能,我在这里设置通知.

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")
        }
    })
Run Code Online (Sandbox Code Playgroud)

谢谢.

Kri*_*aCA 29

要取消所有待处理通知,您可以使用:

UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
Run Code Online (Sandbox Code Playgroud)

要取消特定通知,

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)
}
Run Code Online (Sandbox Code Playgroud)

  • 这是为了取消预定的本地通知。由于“UNUserNotificationCenter.current()”是一个单例对象,因此可以从任何地方调用它 (2认同)

Zac*_*wan 7

相同 的方式UNNotification是基于identifier您创建时传递的方式UNNotificationRequest.

在上面的例子中,

let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger)
Run Code Online (Sandbox Code Playgroud)

你确实有硬编码的identifier"myNotif".这样,只要您想删除已设置的通知,就可以执行以下操作:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif")
Run Code Online (Sandbox Code Playgroud)

但是,请注意,当您对标识符进行硬编码时,每次添加requestUNUserNotificationCenter通知时实际上都会被替换.

例如,如果您"myNotif" request在1分钟后安排了一个集,但是您调用另一个函数来"myNotif"在1小时后安排它,它将被替换.因此,只有最新"myNotif"的一小时后才能进入pendingNotificationRequest.