用户通知取消,swift3 [英] UserNotifications cancel, swift3

查看:119
本文介绍了用户通知取消,swift3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用app,用户可以为每日本地通知设置时间,然后我可以选择启用或禁用这些通知。我的问题如何禁用/取消已设置的通知?

I'm working on app where user can set time for everyday local notification, then I have an option to enable or disable these notifications. My question how I can disable/cancel already setted notification?

在其中一个IF中,我需要取消通知

Here in one of this IFs I need to cancel 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
}

以下是我设置通知的时间表和按钮的功能。

Here are functions for schedule and button where I'm setting 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")
        }
    })

谢谢。

推荐答案

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

For cancelling all pending notifications, you can use this:

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)
}

这篇关于用户通知取消,swift3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆