Как вернуть строковое значение в завершениеХандлера? - PullRequest
0 голосов
/ 17 сентября 2018

У меня есть завершенииHandler типа UNNotificationPresentationOptions , теперь я хочу вернуть строковое значение вместо .alert.

Я хочу показать арабский текст в своем уведомлении, поэтому я хочу установить значение msg в завершенииHandler

    @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 Ответ

0 голосов
/ 17 сентября 2018

Смотрите объявление

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