FCM push notification via AlamoFire - URLSessionTask failed with error: Could not connect to the server

FCM push notification via AlamoFire - URLSessionTask failed with error: Could not connect to the server

我正在尝试实施 FCM 以发送推送通知(使用旧版 api),但我无法这样做。

我已验证我的服务器密钥(从 Firebase 控制台获取)和设备令牌是准确的,并且我已成功通过 Postman 向我的设备发送推送消息。

但是,我无法通过代码这样做。我正在从我的模拟器调用以下代码并“尝试”向我的物理设备发送推送消息。

let urlString: String = "https://fcm/googleapis.com/fcm/send"
let key = "key=[my server key]"

let headers: HTTPHeaders = [
    "Content-Type": "application/json",
    "Authorization": key
]

let notificationParameters: Parameters = [
    "to": "[my push token of physical device]",
    "notification": [
        "title": "My title",
        "body": "My body"
    ]
]

print("1")
AF.request(urlString, method: .post, parameters: 
notificationParameters, encoding: JSONEncoding.default, headers: 
headers).responseJSON { response in
    print("3")
    switch response.result {
    case .success:
        printSuccess("Successfully sent notification")
    case .failure(let error):
        print("Failed to send notification: \(error)")
        print(error.errorDescription)
        print(error.failureReason)
    }
    print("4")
}
print("2")

这是日志:

1
2
2022-03-31 15:23:24.725607-0500 Wurtle with Friends[22207:12775410] 
[connection] nw_socket_handle_socket_event [C3.1:3] Socket SO_ERROR 
[61: Connection refused]
2022-03-31 15:23:24.727572-0500 Wurtle with Friends[22207:12775410] 
Connection 3: received failure notification
2022-03-31 15:23:24.727789-0500 Wurtle with Friends[22207:12775410] 
Connection 3: failed to connect 1:61, reason -1
2022-03-31 15:23:24.728130-0500 Wurtle with Friends[22207:12775410] 
Connection 3: encountered error(1:61)
2022-03-31 15:23:24.729779-0500 Wurtle with Friends[22207:12775409] 
[boringssl] boringssl_metrics_log_metric_block_invoke(153) Failed 
to log metrics
2022-03-31 15:23:24.731134-0500 Wurtle with Friends[22207:12775410] 
Task <B52206D8-E22D-4D8F-B3F5-815692558860>.<1> HTTP load failed, 
0/0 bytes (error code: -1004 [1:61])
2022-03-31 15:23:24.736922-0500 Wurtle with Friends[22207:12775410] 
Task <B52206D8-E22D-4D8F-B3F5-815692558860>.<1> finished with error 
[-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect 
to the server." UserInfo={_kCFStreamErrorCodeKey=61, 
NSUnderlyingError=0x60000279c300 {Error 
Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo=
{_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: 
en1, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, 
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <B52206D8-
E22D-4D8F-B3F5-815692558860>.<1>, 
_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <B52206D8-E22D-4D8F-B3F5-815692558860>.<1>"
), NSLocalizedDescription=Could not connect to the server., 
NSErrorFailingURLStringKey=https://fcm/googleapis.com/fcm/send, 
NSErrorFailingURLKey=https://fcm/googleapis.com/fcm/send, 
_kCFStreamErrorDomainKey=1}
3
Failed to send notification: sessionTaskFailed(error: Error 
Domain=NSURLErrorDomain Code=-1004 "Could not connect to the 
server." UserInfo={_kCFStreamErrorCodeKey=61, 
NSUnderlyingError=0x60000279c300 {Error 
Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo=
{_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: 
en1, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, 
_NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <B52206D8-
E22D-4D8F-B3F5-815692558860>.<1>, 
_NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <B52206D8-E22D-4D8F-B3F5-815692558860>.<1>"
), NSLocalizedDescription=Could not connect to the server., 
NSErrorFailingURLStringKey=https://fcm/googleapis.com/fcm/send, 
NSErrorFailingURLKey=https://fcm/googleapis.com/fcm/send, 
_kCFStreamErrorDomainKey=1})
Optional("URLSessionTask failed with error: Could not connect to 
the server.")
4

这是我第一次实施 FCM,我已经坚持了一段时间,所以非常感谢任何帮助。谢谢

您必须按照 Appdelegate 文件中的 firebase 控制台所述获取 FCM 令牌和刷新令牌:

https://firebase.google.com/docs/cloud-messaging/ios/client

你不应该从 iOS 应用程序发送推送,因为服务器密钥等不安全,而且你必须按照 firebase 的要求添加云消息证书和消息密钥。 并且您还必须具有目标 fcm 令牌(每个设备)。 推送通知发件人必须是拥有所有设备 FCM 令牌的后端。

您必须将每个 ["user" , "device"] 在 Appdelegate 中获得的 FCM 令牌发送到后端,并且您的后端必须为用户设备保存它。

FCM 令牌是每个设备 APNS 令牌的映射键。

尽管我不建议直接从应用程序发送 FCM 请求,但您的服务器 URL 地址似乎有错字。

改成https://fcm.googleapis.com/fcm/send

就可以了