gpt4 book ai didi

IOS FCM未收到数据消息

转载 作者:行者123 更新时间:2023-11-29 05:28:33 24 4
gpt4 key购买 nike

我正在使用 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 中找到了解决方案

最佳答案

 Messaging.messaging().delegate = self  

Messaging.messaging().shouldEstablishDirectChannel = true

//在 didFinishLaunchingWithOptions 中调用此方法,然后检查 token 是否到来

//打印(deviceToken)

//如果仍然没有 token ,那么您必须确保 key 链中的 APNS 证书必须位于 FireBase 云消息传递设置中,除此之外,您的代码似乎没问题,唯一的问题会出现在后端设置中,即您如何在 firebase 上配置您的应用。

Firebase APNS cer

关于IOS FCM未收到数据消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57912356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com