首页 > 解决方案 > SwiftUI/Firebase 静默通知构建/处理

问题描述

我希望就如何在 SwiftUI 中正确发送/接收静默 Firebase 推送通知获得一些帮助。我已经阅读了几个类似的主题,每个主题都对正确构建无声推送通知所需的特殊调味料提出了独特的建议。我想我现在已经尝试了各种线程中推荐的ContentAvailable, content_available, content-available, "apns-priority": 5, "priority": "high",等的所有可能组合。pray_to_the_Push_Gods: true我还尝试使用 API 测试器、Curl 和 Google Cloud Functions 发送消息。

在每种情况下,我总能得到一条正常的消息来发送,它会正确地传递数据负载。在每种情况下,我都无法收到要发送的无声消息。我启用了远程和后台功能。测试设备上的低数据模式已关闭。

例如,发帖

{
  "content_available": true,
  "apns-priority": 5,
  "notification": {
    "title": "Not Silent",
  },
  "data": {
    "sampleData": "7777"
  },
  "to": "[my_FCM_token]",
}

向设备发送罚款,点击通知执行适当的代码以显示我的示例数据。

Message ID: 1628420135470598
Updating userID to: 8888
[AnyHashable("sampleData"): 8888, AnyHashable("gcm.message_id"): 1628420135470598, AnyHashable("aps"): {
    alert =     {
        title = "Not Silent";
    };
    "content-available" = 1;
}, AnyHashable("google.c.sender.id"): 298615355806, AnyHashable("google.c.a.e"): 1, AnyHashable("google.c.fid"): cJqpr3rMAkqMqffuvL4LXe]

但是,如果我发布:

{
  "content_available": true,
  "apns-priority": 5,
  "notification": {
    "empty": "body",
  },
  "data": {
    "sampleData": "5555"
  },
  "to": "[my_FCM_token]",
}

我成功完成(使用 POST 或使用关联的 CURL 代码),但没有证据表明我的消息处理程序接收到静默推送。

func application(_ application: UIApplication,
                   didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                   fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult)
                     -> Void) {

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print("Silent Message ID: \(messageID)")
    }
    
    if let sampleData = userInfo["sampleData"] as? String {
        userID = sampleData
        print ("Updating userID to: \(sampleData)")
    }

    // Print full message.
    print("Silent Message: \(userInfo)")

    completionHandler(UIBackgroundFetchResult.newData)
  }

我也尝试过"notification": { "empty": "body", },完全忽略推送,但没有运气。

鉴于我已经尝试过的一切,我无法弄清楚我是否在配置静默消息的方式上做错了,或者我在 Xcode 中的接收/处理方面做错了什么。

非常感谢任何帮助/指导。

标签: swiftuipush-notificationfirebase-cloud-messagingsilent-notification

解决方案


推荐阅读