2016-09-20 4 views
3

UNNotification 액션을 사용하여 사용자가 "경고"를 다른 사람에게 전자 메일 또는 sms (즉, 작업 위임) 할 수있게하려고합니다. 알림 자체를 클릭하면 포 그라운드로 이동하고 올바른 화면으로 이동하여 문제없이 "경고"를 볼 수 있습니다.iOS 10 UNNotificationAction에서 전자 메일/SMS 보내기

세 가지 사용자 지정 작업 (보기/전자 메일/SMS 보내기,보기가 조금 복잡하지만 여기서는 모든 옵션을 알고 싶습니다)을 추가했습니다. 내 대리자 메서드가 제대로 호출되고 있습니다. userNotificationCenter (_ : didReceive : withCompletionHandler) 문제가 없습니다. 그러나 응용 프로그램은 전경에 오지 않습니다. 작업을 처리하기 위해 MFMailComposeViewController/MFMessageComposeViewController를 가져오고 싶습니다. 경고가 도착할 때 전경에 있으면 모든 것이 완벽하게 작동합니다. 그러나 경고가 도착할 때 백그라운드에서 앱이 백그라운드에 머물러 있고 MFMailComposeViewController/MFMessageComposeViewController 뷰가 표시되지 않습니다.

이 문제를 해결할 수있는 방법이 있습니까? 여기

내가 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) 
} 
에 사용하는 확장 코드 내가 여기에 MFMailComposeViewController

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

에 사용하는 확장 코드 내 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) 
    } 
} 

에서 일부 코드입니다

}

공간을 절약하기 위해 showToast 메소드/AppEventManager 클래스를 포함하지 않았습니다. 당신이

답변

3

확인을 줄 수있는 모든 도움을 사전에

덕분에, 지금은 어리석은 생각합니다. 나는 어제 대답을 찾고 몇 시간을 보냈고 그것을 찾지 못했습니다. 질문을 게시 한 지 5 분 후 나는 아주 간단한 답을 찾는다. 다음과 같이 UNNotificationAction 초기화 프로그램에 .foreground 옵션을 추가하기 만하면됩니다.

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