2016-09-20 2 views
3

Ich versuche, eine UNNotification-Aktion zu verwenden, um dem Benutzer zu ermöglichen, eine "Warnung" per E-Mail an eine andere Person zu senden (d. H. Eine Aktion zu delegieren). Wenn ich auf die Benachrichtigung selbst klicke, kommt sie in den Vordergrund und ich kann auf den rechten Bildschirm gehen und die "Warnung" ohne Probleme sehen.iOS 10 UNNotificationAction E-Mail senden/SMS

Ich habe drei benutzerdefinierte Aktionen hinzugefügt (Ansicht/E-Mail/SMS senden, View ist ein wenig redundant, aber ich wollte, dass sie alle ihre Optionen hier kennen). Meine Delegate-Methode wird richtig aufgerufen userNotificationCenter (_: didReceive: withCompletionHandler) kein Problem. Die App tritt jedoch nicht in den Vordergrund. Ich möchte einen MFMailComposeViewController/MFMessageComposeViewController aufrufen, um die Aktion zu behandeln. Wenn ich bei der Ankunft im Vordergrund bin, funktioniert alles einwandfrei. Wenn ich jedoch im Hintergrund bin, wenn die Warnung eintrifft, bleibt die App im Hintergrund und die Ansichten MFMailComposeViewController/MFMessageComposeViewController werden nicht angezeigt.

Gibt es eine Möglichkeit, dieses Problem zu umgehen?

Hier ist ein Code von meinem AppDelegate

static let VIEW_IDENTIFIER = "VIEW_IDENTIFIER" 
static let EMAIL_IDENTIFIER = "EMAIL_IDENTIFIER" 
static let SMS_IDENTIFIER = "SMS_IDENTIFIER" 
static let ALERT_CATEGORY_IDENTIFIER = "ALERT_CATEGORY_IDENTIFIER" 

if #available(iOS 10.0, *) { 
     print("registerForPushNotification(iOS10)") 
     let unViewAction = UNNotificationAction(identifier: AppDelegate.VIEW_IDENTIFIER, title: "View".localized()) 
     let unEMailAction = UNNotificationAction(identifier: AppDelegate.EMAIL_IDENTIFIER, title: "EMail".localized()) 
     let unSMSAction = UNNotificationAction(identifier: AppDelegate.SMS_IDENTIFIER, title: "SMS".localized()) 

     let unAlertCategory = UNNotificationCategory(identifier: AppDelegate.ALERT_CATEGORY_IDENTIFIER, actions: [unViewAction, unEMailAction, unSMSAction], intentIdentifiers: [], options: [.customDismissAction]) 

     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in 
      // Enable or disable features based on authorization. 
      print("center.requestAuthorization granted(\(granted)) error(\(error))") 
     } 
     center.setNotificationCategories([unAlertCategory]) 
     application.registerForRemoteNotifications() 
    } 

@available(iOS 10.0, *) 
public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Swift.Void) { 
    let notification = response.notification 
    print("userNotificationCenter:didReceive(\(response.actionIdentifier), \(notification.debugDescription))") 
    doCustomAction(identifier: response.actionIdentifier, userInfo: notification.request.content.userInfo) 
    completionHandler() 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Swift.Void) { 
    print("userNotificationCenter:willPresent(\(notification.debugDescription))") 
    completionHandler([.alert, .sound]) 
} 

func doCustomAction(identifier: String, userInfo: [AnyHashable : Any]) { 
    if identifier == AppDelegate.SMS_IDENTIFIER || identifier == AppDelegate.EMAIL_IDENTIFIER { 
     if let current = TitanTabBarController.currentViewController { 
      let json = JSON(userInfo) 
      let alertJSON = json["Alert"] 
      if alertJSON.type != .null { 
       let credentials = Credentials(ip: json["serverIP"].stringValue, type: json["serverType"].stringValue == "Switch" ? .switch : .server) 
       let alert = Alert(credentials: credentials, json: alertJSON) 
       let msg = Alert.FullMessage([alert]) 
       if identifier == AppDelegate.SMS_IDENTIFIER { 
        current.sendMessage(message: msg) 
       } 
       if identifier == AppDelegate.EMAIL_IDENTIFIER { 
        current.sendMessage(message: msg) 
       } 
      } 
     } 
    } else { 
     AppEventManager.instance.post(event: .notificationAction, sender: self, data: userInfo) 
    } 
} 

Hier wird die Erweiterung Code, den ich für die MFMailComposeViewController verwenden

extension UIViewController: MFMailComposeViewControllerDelegate { 
func sendEMail(message: String) { 
    print(message) 
    if MFMailComposeViewController.canSendMail() { 
     let mail = MFMailComposeViewController() 
     mail.mailComposeDelegate = self 
     mail.setSubject("APCON Mobile Alert".localized()) 
     mail.setMessageBody(message, isHTML: false) 

     present(mail, animated: true) { 
      print("MFMailComposeViewControllerDelegate controller.present completion") 
     } 
    } else { 
     showToast(msg: "Mail is not currently available on this device".localized()) 
    } 
} 
public func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { 
    switch (result) { 
    case .cancelled: 
     print("Message was cancelled") 
    case .saved: 
     print("Message was saved") 
    case .sent: 
     print("Message was sent") 
    case .failed: 
     print("Message failed") 
     showToast(msg: "Sending text message failed".localized()) 
    } 
    dismiss(animated: true, completion: nil) 
} 

Hier wird die Erweiterung Code, den ich für die MFMessageComposeViewControllerDelegate

extension UIViewController: MFMessageComposeViewControllerDelegate { 
func sendMessage(message: String) { 
    print(message) 
    if MFMessageComposeViewController.canSendText() { 
     let msg = MFMessageComposeViewController() 
     msg.messageComposeDelegate = self 
     msg.body = message 

     present(msg, animated: true) { 
      print("MFMessageComposeViewControllerDelegate controller.present completion") 
     } 
    } else { 
     showToast(msg: "Texting is not currently available on this device".localized()) 
    } 
} 

public func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) { 
    switch (result) { 
    case .cancelled: 
     print("Message was cancelled") 
    case .failed: 
     print("Message failed") 
     showToast(msg: "Sending text message failed".localized()) 
    case .sent: 
     print("Message was sent") 
    } 
    dismiss(animated: true, completion: nil) 
} 
verwenden

}

Ich habe die showToast-Methoden/AppEventManager-Klasse nicht aufgenommen, um Speicherplatz zu sparen.

Vielen Dank im Voraus für jede Hilfe Sie

Antwort

3

OK geben kann, ich fühle mich jetzt dumm. Ich habe gestern mehrere Stunden nach der Antwort gesucht und sie nicht gefunden. 5 Minuten nach dem Posten der Frage finde ich eine sehr einfache Antwort. Fügen Sie einfach die Option .foreground im UNNotificationAction-Initialisierer wie folgt hinzu.

  let unViewAction = UNNotificationAction(identifier: AppDelegate.VIEW_IDENTIFIER, title: "View".localized(), options: [.foreground]) 
     let unEMailAction = UNNotificationAction(identifier: AppDelegate.EMAIL_IDENTIFIER, title: "EMail".localized(), options: [.foreground]) 
     let unSMSAction = UNNotificationAction(identifier: AppDelegate.SMS_IDENTIFIER, title: "SMS".localized(), options: [.foreground])