【问题标题】:Parsing Push Notification Data in swift快速解析推送通知数据
【发布时间】:2020-06-03 12:31:27
【问题描述】:

我收到了推送通知,它们在 iPhone 上显示得非常好。但由于某种原因,我想解析我在 didReceivedRemoteNotification 方法中收到的以下结果以进行推送通知。

 func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {


    print("FFTF", "didReceiveRemoteNotification", userInfo)
  }

以下是我打印 userInfo 时得到的结果

    [AnyHashable("FcmNotificationStatus"): 3, AnyHashable("ListItemIds"): ["42D890C4-0804-4434-B137-0EECDEFEC319"], AnyHashable("UserId"): 15, AnyHashable("gcm.message_id"): 1582109***6, AnyHashable("Title"): MY App, AnyHashable("body"): Welcome., AnyHashable("aps"): {
    "content-available" = 1;
}, AnyHashable("google.c.sender.id"): 589****85]

这是我收到的回复,我想从中获得以下具有特定类型的字段

  • 内容可用(整数)
  • FcmNotificationStatus (Int)
  • ListItemIds(字符串数组)
  • 用户 ID(整数)

我尝试了很多方法,但我的应用程序崩溃了。我可以做些什么来轻松解析它吗?请让我知道该怎么做。

【问题讨论】:

    标签: ios swift iphone xcode push-notification


    【解决方案1】:

    你有一本字典。解析应该是相当直接的。

    let status = userInfo["FcmNotificationStatus"] as? Int
    let ids = userInfo["ListItemIds"] as? [Int]
    let title = userInfo["Title"] as? String
    let body = userInfo["body"] as? String
    

    如果您不显示您的代码并解释您遇到的错误,我无法更详细地回答您为什么会收到这些错误

    This guide 解释了一些概念。专家...

    @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
    
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
    
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
          print("Message ID: \(messageID)")
        }
    
        // Print full message.
        print(userInfo)
    
        // Change this to your preferred presentation option
        completionHandler([])
      }
    
      func userNotificationCenter(_ center: UNUserNotificationCenter,
                                  didReceive response: UNNotificationResponse,
                                  withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
          print("Message ID: \(messageID)")
        }
    
        // Print full message.
        print(userInfo)
    
        completionHandler()
      }
    }
    

    您不会总是在 didReceiveRemoteNotification 中收到通知,您应该使用链接文章中提到的委托方法。

    【讨论】:

    • 又添加了一个字段,我试过了,但它给了我错误
    • 请再次查看问题
    • apsJSON。所以你只需要这样解析它
    • 我试过你上面给定的行,每件事都返回为 nil
    • 让我看看你刚刚放的指南
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多