Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 10未通知操作发送电子邮件/短信_Ios_Swift_Notifications - Fatal编程技术网

iOS 10未通知操作发送电子邮件/短信

iOS 10未通知操作发送电子邮件/短信,ios,swift,notifications,Ios,Swift,Notifications,我正在尝试使用未通知操作,以允许用户通过电子邮件/短信向其他人发送“警报”(即委派操作)。当我点击通知本身时,它会出现在前台,我可以进入右侧屏幕,毫无问题地查看“警报” 我添加了三个自定义操作(查看/电子邮件/发送短信,查看有点多余,但我想让他们知道这里的所有选项)。我的委托方法正被正确调用userNotificationCenter(uuIdReceive:withCompletionHandler)没有问题。然而,该应用程序并没有出现在前台。我想使用MFMailComposeViewCont

我正在尝试使用未通知操作,以允许用户通过电子邮件/短信向其他人发送“警报”(即委派操作)。当我点击通知本身时,它会出现在前台,我可以进入右侧屏幕,毫无问题地查看“警报”

我添加了三个自定义操作(查看/电子邮件/发送短信,查看有点多余,但我想让他们知道这里的所有选项)。我的委托方法正被正确调用userNotificationCenter(uuIdReceive: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)
}
以下是我用于MFMessageComposeViewController Delegate的扩展代码

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类


提前感谢您能给予的任何帮助

好的,我现在觉得很傻。昨天我花了几个小时寻找答案,但没有找到。发帖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])