用于安排通知的唯一标识符,Swift 3 iOS 10
Unique Identifier for scheduling notifications, Swift 3 iOS 10
我正在使用唯一标识符安排通知,因此我不必为每个通知创建新的字符串。这一切都适用于安排,但问题在于试图取消它们。
这是我安排通知的代码...
let notifIdentifier = TaskManager.notification2.userInfo.description as String!
let trigger = UNCalendarNotificationTrigger(dateMatching: components , repeats: true)
let request = UNNotificationRequest(identifier: notifIdentifier! , content: TaskManager.notification2, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
这是取消通知的代码...
// Deletion of Cells ...
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let managedObject: NSManagedObject = frc.object(at: indexPath) as! NSManagedObject
context.delete(managedObject)
if tableView == TaskTableViews {
let itemController = TaskManager()
let nItem: List = frc.object(at: indexPath) as! List
itemController.nItem = nItem
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [itemController.notifIdentifier!] )
当我尝试取消它们时,它们很碰运气。我通过将标识符更改为常规字符串来测试代码,一切正常,因此它绝对是唯一标识符。
关于为每个新的 task/notification 创建唯一 ID 有什么想法/建议吗?
安排通知
@IBAction func scheduleNotification(_ sender: AnyObject) {
let uuid = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Example", arguments: nil)
content.sound = UNNotificationSound.default()
//...
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: self.datePicker.date)
dateComponents.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let errorPerforms = error {
print(errorPerforms.localizedDescription)
} else {
print("Success")
}
}
let managedObject = ManagedObject(context: self.managedObjectContext!)
managedObject.setValue(uuid, forKey: "uuid")
//...
do {
try self.managedObjectContext.save()
self.dimiss()
} catch {}
}
从indexPath
删除通知。
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])
do {
try self.managedObjectContext.save()
self.tableView.reloadData()
} catch {}
default:
break
}
}
这很好用!当您使用 NSFetchedResultsControllerDelegate
协议方法时。希望对你有帮助
我正在使用唯一标识符安排通知,因此我不必为每个通知创建新的字符串。这一切都适用于安排,但问题在于试图取消它们。
这是我安排通知的代码...
let notifIdentifier = TaskManager.notification2.userInfo.description as String!
let trigger = UNCalendarNotificationTrigger(dateMatching: components , repeats: true)
let request = UNNotificationRequest(identifier: notifIdentifier! , content: TaskManager.notification2, trigger: trigger)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
这是取消通知的代码...
// Deletion of Cells ...
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let managedObject: NSManagedObject = frc.object(at: indexPath) as! NSManagedObject
context.delete(managedObject)
if tableView == TaskTableViews {
let itemController = TaskManager()
let nItem: List = frc.object(at: indexPath) as! List
itemController.nItem = nItem
UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [itemController.notifIdentifier!] )
当我尝试取消它们时,它们很碰运气。我通过将标识符更改为常规字符串来测试代码,一切正常,因此它绝对是唯一标识符。
关于为每个新的 task/notification 创建唯一 ID 有什么想法/建议吗?
安排通知
@IBAction func scheduleNotification(_ sender: AnyObject) {
let uuid = UUID().uuidString
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Example", arguments: nil)
content.sound = UNNotificationSound.default()
//...
var dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute], from: self.datePicker.date)
dateComponents.second = 0
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: uuid, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in
if let errorPerforms = error {
print(errorPerforms.localizedDescription)
} else {
print("Success")
}
}
let managedObject = ManagedObject(context: self.managedObjectContext!)
managedObject.setValue(uuid, forKey: "uuid")
//...
do {
try self.managedObjectContext.save()
self.dimiss()
} catch {}
}
从indexPath
删除通知。
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
let managedObject = self.fetchedResultsController.object(at: indexPath)
self.managedObjectContext.delete(managedObject)
let center = UNUserNotificationCenter.current()
center.removeDeliveredNotifications(withIdentifiers: [managedObject.uuid!])
center.removePendingNotificationRequests(withIdentifiers: [managedObject.uuid!])
do {
try self.managedObjectContext.save()
self.tableView.reloadData()
} catch {}
default:
break
}
}
这很好用!当您使用 NSFetchedResultsControllerDelegate
协议方法时。希望对你有帮助