2017-04-09 2 views
0

Ich habe eine lokale Benachrichtigung und ich habe eine Aktion hinzugefügt. Wie soll ich vorgehen, dass beim Tippen auf die Aktion die lokale Benachrichtigung nach 15 Minuten erneut ausgelöst wird?Lokale Benachrichtigung in iOS 10 verschieben

Hier ist der Code und die Erweiterung so, dass die Anmeldung kann unter Verwendung eines UIImage gezeigt werden:

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

Antwort

0

nur eine modifizierte Version scheduleNotification wieder in der Action-Handler aufrufen, wenn der Benutzer die Aktion aktiviert. Ich würde definitiv eine Funktionsvariable als TimeInterval oder einen boolean, der die Auslösezeit der Benachrichtigung bestimmt, erstellen.

+0

Ich habe darüber nachgedacht, aber wie kann ich den aktuellen Artikel bekommen, wenn die App geschlossen ist? – user7696382

Verwandte Themen