I am trying to use a UNNotification action to allow the user to e-mail/sms an "Alert" to someone else (i.e. delegate an action). When I click on the notification itself it comes to the foreground and I can go to the right screen and view the "Alert" without problems.
I have added three custom actions (View/E-Mail/Send SMS, View is a bit redundant but I wanted them to know all their options here). My delegate method is being called properly userNotificationCenter(_:didReceive:withCompletionHandler) no problem. However, the app does not come to the foreground. I would like to bring up a MFMailComposeViewController/MFMessageComposeViewController in order to handle the action. If I am in the foreground when the alert arrives everything works perfectly. However, if I am in the background when the alert arrives the app stays in the background and the MFMailComposeViewController/MFMessageComposeViewController views do not come up.
Is there a way to work around this problem?
Here is some code from my 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)
}
}
Here is the extension code I use for the 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)
}
Here is the extension code I use for the 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)
}
}
I did not include the showToast methods/AppEventManager class to save space.
Thanks in advance for any help you can give
OK, I feel silly now. I spent several hours looking for the answer yesterday and did not find it. 5 minutes after posting the question I find a very simple answer. Just add the .foreground option in the UNNotificationAction initializer as follows.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With