您好我正在尝试向使用Swift订阅特定主题的所有设备发送通知。我想通过代码而不是Firebase控制台手动发送推送。这是我写的代码。
public static func sendPush(){
if let url = URL(string: "https://fcm.googleapis.com/fcm/send"){
var urlRequest: URLRequest = URLRequest(url: url)
urlRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.addValue("key = AIzaSyBaaaaaaaaaaaa-NbAUq3iMYmw", forHTTPHeaderField: "Authorization")
let dic: Dictionary<String, Any> = [
"to" : "/topics/News",
"priority" : "high",
"notification" :[
"body" : "This is a notification",
"title" : "FCM Notification"
]
]
do{
let data = try JSONSerialization.data(withJSONObject: dic, options: .prettyPrinted)
urlRequest.httpBody = data
urlRequest.httpMethod = "POST"
print("JSON Object Serialized")
}
catch{
}
let task = URLSession.shared.dataTask(with: urlRequest, completionHandler: {
data, response, error in
print("Inside Task")
if error != nil{
print("Push \(error?.localizedDescription)")
}
if response != nil {
print("Data : \(response)")
}
})
task.resume()
}
}
当我通过调用它执行上述功能时,我得到状态代码200.这意味着没问题,请求成功。但订阅此主题的我的设备不会收到通知。 我也订阅了通知,但没有任何反应。 当我尝试使用Firebase控制台向各个设备发送通知时,他们会收到通知,但不会通过代码收到通知。 我正在阅读此文档Tutorial
这是在请求完成后在控制台中打印的内容
Data : Optional(<NSHTTPURLResponse: 0x170225560> { URL: https://fcm.googleapis.com/fcm/send } { status code: 200, headers {
"Cache-Control" = "private, max-age=0";
"Content-Encoding" = gzip;
"Content-Length" = 54;
"Content-Type" = "application/json; charset=UTF-8";
Date = "Fri, 12 May 2017 21:14:52 GMT";
Expires = "Fri, 12 May 2017 21:14:52 GMT";
Server = GSE;
"alt-svc" = "quic=\":443\"; ma=2592000; v=\"37,36,35\"";
"x-content-type-options" = nosniff;
"x-frame-options" = SAMEORIGIN;
"x-xss-protection" = "1; mode=block";
} })
请帮助我解决这个问题,并告诉我我做错了什么,我应该如何取得成功?谢谢。