2016-11-12 92 views
4

我正在开发应用程序,其中用户可以为每天的本地通知设置时间,然后我可以选择启用或禁用这些通知。我的问题如何禁用/取消已设置的通知?UserNotifications cancel,swift3

在这里,在这个国际单项体育联合会,我需要取消通知符

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

谢谢。

+0

嗨,你可以检查我的答案。随意评论任何有关这个问题或我的回答的讨论 – KrishnaCA

回答

16

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

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

这可以帮助我很多,谢谢! – deniskakacka

+0

@deniskakacka,我希望这能回答你的问题 – KrishnaCA

+0

是的它现在工作。再次感谢 – deniskakacka

-3

如前所述here你可以取消所有通知用下面的代码:

UIApplication.sharedApplication().cancelAllLocalNotifications()

5

方式相同UNNotification是识别码是基于identifier当您创建UNNotificationRequest时通过。

在你上面的例子,

let request = UNNotificationRequest(identifier: "myNotif", content: notif, trigger: dateTrigger) 

您实际上已经硬编码了identifier"myNotif"。这样一来,只要你想删除已设置的通知,你可以这样做:

UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: "myNotif") 

但是,请注意,当你硬编码标识,每次当您添加requestUNUserNotificationCenter通知实际上是被更换。

例如,如果您计划在1分钟后设置"myNotif"request,但您调用另一个函数在1小时后安排"myNotif"它将被替换。因此,在一小时后只有最新的"myNotif"将在pendingNotificationRequest

+0

非常感谢。 – deniskakacka