首页 > 解决方案 > 从 Firebase 云消息传递接收自定义数据(布尔型、整数型)

问题描述

我正在通过 curl 向我的 iOS 应用程序发送自定义FCM消息(不仅仅是标题和正文),如下所示。

curl -X POST --header "Authorization: key=myServerKey" \
--Header "Content-Type: application/json" \
https://fcm.googleapis.com/fcm/send \
-d @- << EOF
{
  "to": "my device token",
    "data": {
        "urlFlag": true,
        "category": "Electric",
        "messageId": 1238884523
    },
    "notification": {
        "title": "Test"
        "body": "What do you want?"
    }
}
EOF

我通过以下userNotificationCenter(_:willPresent:withCompletionHandler:)方法收到推送消息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 urlFlag = userInfo["urlFlag"] as? Bool {
            print("Flag: \(urlFlag)") // no good
        }
        if let category = userInfo["category"] as? String {
            print("category: \(category)") // good
        }
        if let messageId = userInfo["messageId"] as? Int {
            print("ID: \(messageId)") // no good
        }
    }
}

该应用程序确实收到标题和正文没有问题。如果自定义数据集包含布尔类型、字符串类型、数字类型,应用程序将只接收String一个。如何让我的应用接收布尔类型和数字类型?谢谢。

编辑

userInfo返回以下内容。

[AnyHashable("google.c.fid"): cgthRgMJPEkaidXXhrXuqv, AnyHashable("urlFlag"): true, AnyHashable("messageId"): 1238884523, AnyHashable("aps"): {

    alert =     {

        body = "What do you want?";

        title = Test;

    };

}, AnyHashable("google.c.sender.id"): 1073343011223, AnyHashable("category"): Electric, AnyHashable("google.c.a.e"): 1, AnyHashable("gcm.message_id"): 1627387811171401]

标签: swiftfirebase-cloud-messagingapple-push-notificationsappdelegate

解决方案


推荐阅读