2017-04-09 120 views
0

我有本地通知,並向其中添加了一個操作。我應該怎麼做,以便在動作被點擊時,15分鐘後再次觸發本地通知?推遲iOS中的本地通知10

這裏是代碼和擴展,以便通知可以使用的UIImage來表明:

extension UNNotificationAttachment { 

    static func create(identifier: String, image: UIImage, options: [NSObject : AnyObject]?) -> UNNotificationAttachment? { 
     let fileManager = FileManager.default 
     let tmpSubFolderName = ProcessInfo.processInfo.globallyUniqueString 
     let tmpSubFolderURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(tmpSubFolderName, isDirectory: true) 
     do { 
      try fileManager.createDirectory(at: tmpSubFolderURL, withIntermediateDirectories: true, attributes: nil) 
      let imageFileIdentifier = identifier+".png" 
      let fileURL = tmpSubFolderURL.appendingPathComponent(imageFileIdentifier) 
      guard let imageData = UIImagePNGRepresentation(image) else { 
       return nil 
      } 
      try imageData.write(to: fileURL) 
      let imageAttachment = try UNNotificationAttachment.init(identifier: imageFileIdentifier, url: fileURL, options: options) 
      return imageAttachment 
     } catch { 
      print("error " + error.localizedDescription) 
     } 
     return nil 
    } 
} 


func scheduleNotification() { 
    removeNotification() 
    if shouldRemind && dueDate > Date() { 
     let content = UNMutableNotificationContent() 
     content.title = "Reminder:" 
     content.body = text 
     content.sound = UNNotificationSound.default() 
     let calendar = Calendar(identifier: .gregorian) 
     let components = calendar.dateComponents([.month, .day, .hour, .minute], from: dueDate) 
     let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false) 
     let identifier = ProcessInfo.processInfo.globallyUniqueString 
     if let attachment = UNNotificationAttachment.create(identifier: identifier, image: notificationImage, options: nil) { 
      content.attachments = [attachment] 
     } 
     let action = UNNotificationAction(identifier: "remindLater", title: "Remind me later", options: []) 
     let category = UNNotificationCategory(identifier: "category", actions: [action], intentIdentifiers: [], options: []) 
     UNUserNotificationCenter.current().setNotificationCategories([category]) 
     content.categoryIdentifier = "category" 
     let request = UNNotificationRequest(identifier: "\(itemID)", content: content, trigger: trigger) 
     let center = UNUserNotificationCenter.current() 
     center.add(request) 
    } 
} 

回答

0

只需在操作處理程序再次調用修改後的版本scheduleNotification當用戶激活動作。我肯定會創建一個函數變量作爲TimeInterval或確定通知觸發時間的布爾值。

+0

我已經想到了這一點,但如果應用程序關閉,我該如何獲取當前項目? – user7696382