swift - 从 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]
解决方案
推荐阅读
- spring - Mockito 测试用例(出现异常 - 缺少方法调用)
- python - 如何从python中的文本中获取重要的关键字
- function - 如何根据谷歌表格上单元格的颜色更新单元格?
- reactjs - 使用 useState 挂钩更新状态的问题
- python-3.x - 将嵌套的 JSON 数组转换为 pandas 数据框
- node.js - nodejs 而不是 localhost 显示其他 URL
- algorithm - 为什么分而治之的反转计数为 n*log(n)?
- maven - 需要知道我的项目是否是 web 应用程序项目是否是 maven
- django - Django orm 查询访问值
- php - infyOm 的样板无法设置 DataTables [Laravel 6.0 using AdminLTE Boilerplate]