I'm trying to implement FCM to send a push notification (using the legacy api), but am unable to do so.
I have verified that my server key (taken from Firebase console) and device token are accurate, and I have successfully sent a push note to my device via Postman.
However, I am unable to do so via code. I am calling the below code from my simulator and "attempting" to send a push note to my physical device.
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")
Here's the log:
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
This is my first time implementing FCM and I've been stuck on it for a while now, so any help is appreciated. Thank you
Even though I do not recommend sending an FCM request straight from the app, it appears you have a typo in your server URL address.
Just change it to https://fcm.googleapis.com/fcm/send