2017-01-29 41 views
0

我使用唯一標識符調度通知,因此我不必爲每個通知創建一個新的字符串。這一切都適用於調度,但問題在於試圖取消它們。調度通知的唯一標識符,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!]) 

當我嘗試取消他們,他們都非常擊中,小姐。我已經通過將標識符更改爲常規字符串來測試代碼,並且所有工作都是如此,因此它絕對是唯一標識符。

有關爲每個新任務/通知創建唯一ID的任何想法/建議?

+0

有什麼問題?什麼「他們非常受打擊和想念」甚至_mean?_請解釋什麼是問題。什麼是TaskManager,無論如何? – matt

+0

一次只能取消一個通知(不是多個)。如果您安排通知,關閉應用程序,然後重新打開應用程序以刪除任務,則通知不會被取消。就像我說的,這一切都完美地與字符串一起工作,但使用唯一的ID是我的問題。我正在尋找另一種方式爲每個任務創建唯一的ID。 – Tai

+0

「所有的作品完美地與一個字符串,但使用唯一的ID是我的問題」我無法弄清楚這是什麼意思。標識符_is_是一個字符串,所以我沒有得到你正在繪製的區別。你還沒有展示你的標識符是如何創建和存儲的,所以誰知道你在做什麼?到目前爲止,整個問題完全不清楚(至少對我而言)。 – matt

回答

0

調度通知

indexPath
@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 {} 
} 

刪除通知。

// 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協議方法時。希望它有幫助

+0

不幸的是,這並沒有爲我工作。我可以與您聯繫並進一步討論嗎? – Tai

+0

好的!我如何才能幫助你? – Mannopson

+1

謝謝,我得到它的工作! – Tai