代码之家  ›  专栏  ›  技术社区  ›  iDeveloper

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

  •  2
  • iDeveloper  · 技术社区  · 6 年前

    我有一个 完成处理程序 类型 未通知表示选项 ,现在我要返回字符串值,而不是.alert。

    我想在通知中显示阿拉伯语文本,所以我想设置 味精 completionHandler中的值

        @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
    
    
    
            }
    
            }
        }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Shehata Gamal    6 年前

    期待宣言

    @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,您不能将委托方法签名更改为所需的签名,其主要目的是返回系统将为此即将到来的通知触发的权限

    //

    enter image description here

    在此处执行编辑

    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)
        }
    }