UNNotificationアクションを使用して、ユーザーが他の人に "警告"を電子メール/ SMSで送信できるようにしようとしています(つまり、操作を委任しました)。通知自体をクリックすると、フォアグラウンドが表示され、正しい画面に移動して「警告」を問題なく表示できます。iOS 10 UNNotificationActionは電子メール/ SMSを送信します
私は3つのカスタムアクションを追加しました(ビュー/ Eメール/ SMS送信、ビューは少し冗長ですが、ここではすべてのオプションを知りたがっていました)。デリゲートメソッドが適切に呼び出されていますuserNotificationCenter(_:didReceive:withCompletionHandler)問題はありません。しかし、アプリケーションはフォアグラウンドに来ていません。私は、アクションを処理するためにMFMailComposeViewController/MFMessageComposeViewControllerを呼び出すと思います。アラートが到着したときに私が前景にいると、すべてが完全に機能します。しかし、警告が到着したときに私がバックグラウンドにいると、アプリケーションはバックグラウンドにとどまり、MFMailComposeViewController/MFMessageComposeViewControllerビューは表示されません。
この問題を回避する方法はありますか?
ここに私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)
}
}
からいくつかのコードがここ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)
}
のため、私が使用して拡張コードは、ここで私は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)
}
に使用する延長コードがあるされています
}
私はスペースを節約するためにshowToastメソッド/ AppEventManagerクラスを含んでいませんでした。あなたは