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

标签 ios swift push-notification

我有一个类型为 UNNotificationPresentationOptionscompletionHandler,现在我想返回字符串值,而不是 .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



        }

        }
    }

最佳答案

看声明

@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 ,您不能将委托(delegate)方法签名更改为您想要的,它的主要目的是返回系统将针对即将到来的通知触发的权限

//

您可以使用通知服务&&内容扩展

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

关于ios - 如何在 completionHandler 中返回字符串值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52369309/

相关文章:

ios - 弹出窗口中的 UIActionSheet 布局错误

ios - 'isRunningInStoreDemoMode' 什么时候为真?

ios - 如何从键盘上删除顶部栏?

iOS swift 使用 alamofire 在应用程序和共享扩展之间共享 cookie

android - 编译/部署期间 iOS 模拟器错误 "Failed to launch the simulator: Array index is out of range"(MT1008)

当 UITextField.secureTextEntry 改变它的值时 Swift 得到回调

ios - 保持 UNNotificationContent 几秒钟

iOS/Obj-C - didReceiveRemoteNotification 语句没有触发?

ios - 制作 Parse 推送生产证书 - iOS

android - 如何在 android 中启用和禁用 GCM?