【问题标题】:IOS FCM unreceived data messageIOS FCM 未收到数据消息
【发布时间】: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


    【解决方案1】:
     Messaging.messaging().delegate = self  
    
    Messaging.messaging().shouldEstablishDirectChannel = true
    

    //在didFinishLaunchingWithOptions中调用这个,然后检查token是否来了

    // 打印(deviceToken)

    // 如果仍然没有令牌,那么您必须确保您的密钥链中的 APNS 证书必须在 FireBase 云消息传递设置中,除了您的代码看起来很好之外,只有后端设置会出现问题,即您如何在 Firebase 上配置您的应用。

    Firebase APNS cer

    【讨论】:

    • 我认为存在误解,如果我将通知标签放在我的有效负载中,它就可以正常工作。
    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多