I am having some problems with sending fcm notifications directly from my SwiftUI app.
When I try to send a notification to token like this, it will work ok:
let request1 = NSMutableURLRequest(url: URL(string: "https://fcm.googleapis.com/fcm/send")!)
request1.httpMethod = "POST"
request1.setValue("application/json", forHTTPHeaderField: "Content-Type")
request1.setValue("\(serverKey)", forHTTPHeaderField: "Authorization")
let json = [
"to": "my token",
"notification": [
"title": "Notification Title",
"body": "Notification Body"
]
] as [String : Any]
let jsonData = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
request1.httpBody = jsonData
URLSession.shared.dataTask(with: request1 as URLRequest) { (data, response, error) in
if error != nil {
print(error!)
return
}
print(response!)
}.resume()
This code works, but when I change the line "to": "my token" into
"topic": "/topics/test_topic"
or
"topic": "test_topic"
It won't send notification to any of the devices subscribed to topic. Devices are connected to topic, because if I send notification to topic from Firebase console, it works OK. Is there any way to send a notification to topic directly from a SwiftUI app?
I would like to be able to send notifications from a SwiftUI app directly, without backend servers.