Ios 如何在completionHandler中返回字符串值?

Ios 如何在completionHandler中返回字符串值?,ios,swift,push-notification,Ios,Swift,Push Notification,我有一个类型为UNNotificationPresentationOptions的completionHandler,现在我想返回字符串值,而不是.alert 我想在通知中显示阿拉伯语文本,因此我想在completionHandler中设置msg值 @available(iOS 10, *) extension AppDelegate : UNUserNotificationCenterDelegate { // Receive displayed notifi

我有一个类型为UNNotificationPresentationOptions的completionHandler,现在我想返回字符串值,而不是.alert

我想在通知中显示阿拉伯语文本,因此我想在completionHandler中设置msg

    @available(iOS 10, *)
    extension AppDelegate : UNUserNotificationCenterDelegate {

        // Receive displayed notifications for iOS 10 devices.
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let userInfo = notification.request.content.userInfo
            print(userInfo)

            let userInfoa = notification.request.content
            let sd = userInfoa.title

            if let jsonResult = userInfo as? Dictionary<String, AnyObject> {


            var msg = ""
            if let aps_Data = userInfo as? Dictionary<String, AnyObject> {
                if let ar_message = aps_Data["gcm.notification.body_ar"] {

                    print(ar_message)
                    msg = ar_message as! String

                }
            }
            let content:UNNotificationPresentationOptions = msg

            completionHandler([content])

          //completionHandler([.alert]) .  *I dont want use  .alert



        }

        }
    }
@可用(iOS 10,*)
扩展AppDelegate:UNUserNotificationCenterDelegate{
//接收iOS 10设备的显示通知。
func userNotificationCenter(center:UNUserNotificationCenter,
将提交通知:未通知,
withCompletionHandler completionHandler:@escaping(UNNotificationPresentationOptions)->Void){
让userInfo=notification.request.content.userInfo
打印(用户信息)
让userInfoa=notification.request.content
设sd=userInfoa.title
如果让jsonResult=userInfo作为字典{
var msg=“”
如果让aps_Data=userInfo作为字典{
如果let ar_message=aps_Data[“gcm.notification.body_ar”]{
打印(ar_信息)
msg=ar_消息为!字符串
}
}
让内容:UNNotificationPresentationOptions=msg
completionHandler([内容])
//completionHandler([.alert])。*我不想使用.alert
}
}
}
查看声明

@available(iOS 10.0, *)
public struct UNNotificationPresentationOptions : OptionSet {

   public init(rawValue: UInt)


  public static var badge: UNNotificationPresentationOptions { get }

  public static var sound: UNNotificationPresentationOptions { get }

  public static var alert: UNNotificationPresentationOptions { get }
}
这些是权限类型。alert/.sound/.badge,您不能将委托方法签名更改为您想要的,它的主要目的是返回系统将为此即将到来的通知触发的权限

//

您可以使用通知服务和内容扩展(&C)

在这里实现您的编辑

override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

    if let bestAttemptContent = bestAttemptContent {
        // Modify the notification content here...
        bestAttemptContent.title = "\(bestAttemptContent.title) [modified]"

        contentHandler(bestAttemptContent)
    }
}

这似乎是一个本机委托方法。在这种情况下,如果更改完成处理程序,它将不再响应。您可能希望在接收警报的位置处理警报。为什么要返回字符串?我想用阿拉伯语显示通知,但默认情况下它是用英语显示的。您需要从一开始就对其进行预本地化,是推送还是本地?推送通知,您能建议更好的预本地化方法吗?我已经添加了这个,但是没有调用类服务扩展。您是否在通知json中设置了mutable content=1检查此项,如果没有,扩展类将不会调用?请参阅我的另一个问题以获得更好的理解-我认为此响应来自firebase,如果是,请尝试带有.pem证书的本机usng php脚本,然后尝试使用附加键,按照教程中的每个步骤进行操作