gpt4 book ai didi

iOS 10 UNNotificationAction 发送电子邮件/短信

转载 作者:搜寻专家 更新时间:2023-10-31 22:30:21 27 4
gpt4 key购买 nike

我正在尝试使用 UNNotification 操作来允许用户通过电子邮件/短信向其他人发送“警报”(即委托(delegate)操作)。当我单击通知本身时,它会出现在前台,我可以转到正确的屏幕并毫无问题地查看“警报”。

我添加了三个自定义操作(查看/电子邮件/发送短信,查看有点多余,但我希望他们在这里了解所有选项)。我的委托(delegate)方法被正确调用 userNotificationCenter(_:didReceive:withCompletionHandler) 没问题。但是,该应用程序不会出现在前台。我想调出一个 MFMailComposeViewController/MFMessageComposeViewController 来处理这个 Action 。如果警报到达时我在前台,则一切正常。但是,如果当警报到达时我在后台,应用程序将保留在后台并且 MFMailComposeViewController/MFMessageComposeViewController View 不会出现。

有没有办法解决这个问题?

这是我的 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 类。

在此先感谢您提供的任何帮助

最佳答案

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

关于iOS 10 UNNotificationAction 发送电子邮件/短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598901/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com