1

I am getting push notification and they are getting displayed on iPhone very nicely. But for some reason I want to parse the following result that I am receiving in didReceivedRemoteNotification method for push notification.

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


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

Following is what I get when I print the 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]

This is response I am getting and from it I want to get the following fields that have specific types

  • content-available (Int)
  • FcmNotificationStatus (Int)
  • ListItemIds (String Array)
  • UserId (Int)

And I tried many things but I am getting my application crash. Is there anything that I can do to parse it easily? Please let me know how to do it.

1 Answer 1

2

What you have there is a dictionary. Parsing should be fairly straight forward.

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

I cannot give a more detailed answer as to why you are getting these errors without you showing your code and explaining what errors your getting

This guide explains some of the concepts. An excert...

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

You will not always receive notifications in didReceiveRemoteNotification, you should use the delegate methods mentioned in the linked article.

Sign up to request clarification or add additional context in comments.

9 Comments

one more field is added and I tried this and it has given me error
please see question agaain
aps is JSON. So you just need to parse it as such
I tried your above given line, every thing is returned as nil
let me see the guide you have just put
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.