Receiving Custom Data from Firebase Cloud Messaging Through Curl

I'm trying to receive a message from Firebase Cloud Messaging. I'm sending a message with curl as follows.

curl -X POST --header "Authorization: key=myServerKey" \
--Header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
-d @- << EOF
{
  "to": "my device token",
  "notification": {
        "title": "TestMe"
        "body": "I hate you more than ever!"
  },
  "data": {
    "category" : "Electric",
    "messageId" : 2356565
  }
}
EOF

It goes out without a problem. And my app gets a call in AppDelegate. The following is part of my AppDelegate.

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        /* Firebase Cloud Messaging */
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        Messaging.messaging().token { token, error in
            if let error = error {
                print("Error fetching FCM registration token: \(error)")
            } else if let token = token {
                if self.showFirebaseToken {
                    print("Remote FCM registration token: \(token)")
                }
            }
        }
        
        if #available(iOS 10.0, *) {
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self
            
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()
        
        return true
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID)")
        }
        completionHandler([[.alert, .sound]])
        
        if let aps = userInfo["aps"] as? [String: Any], let alert = aps["alert"] as? [String: Any] {
            if let title = alert["title"] as? String, let body = alert["body"] as? String {
                print("title: \(title) body: \(body)") // good
            }
        }
        
        if let data = userInfo["data"] as? [String: Any] {
            if let category = data["category"] as? String, let id = data["messageId"] as? Int {
                print("Category: \(category) id: \(id)") // not good
            }
        } else {
            print("Heck!") // that's what my app gets
        }

        if let aps = userInfo["aps"] as? [String: Any], let data = aps["data"] as? [String: Any] {
            ...
            ...
        } else {
            print("Heck!") // that's what my app gets
        }
        
        print("You've received a message")
    }
}

My iOS app only receives the notification part (title, body). It won't receive the data part. What am I doing wrong? Thanks.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum