【发布时间】:2020-01-14 15:57:03
【问题描述】:
我正在使用 FCM 向 iOS 移动应用程序发送自定义数据消息,如果负载是这样的,我没有收到任何消息:
{
content_available: true,
data : {
title : message_title,
body : message_body,
}
}
但如果我添加这样的有效负载,我会收到通知:
{
content_available: true,
notification : {
title : message_title,
body : message_body,
},
data : {
title : message_title,
body : message_body,
}
}
但我不想发送通知我想发送数据消息。
我的 appDelegate 是:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
application.registerForRemoteNotifications()
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })
application.applicationIconBadgeNumber = 0
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Messaging.messaging().apnsToken = deviceToken
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
UIApplication.shared.applicationIconBadgeNumber = 0
let userInfo = notification.request.content.userInfo
print("[UserNotificationCenter] applicationState: \(applicationStateString) willPresentNotification: \(userInfo)")
NotificationCenter.default.post(name: .didReceiveData, object: nil, userInfo: userInfo)
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
UIApplication.shared.applicationIconBadgeNumber = 0
let userInfo = response.notification.request.content.userInfo
print("[UserNotificationCenter] applicationState--: \(applicationStateString) didReceiveResponse: \(userInfo)")
completionHandler()
}
为什么数据消息不起作用?
我在这个帖子https://stackoverflow.com/a/51954446/1747440找到了解决方案
【问题讨论】:
标签: ios swift firebase firebase-cloud-messaging