I want to sent a push notification from device to device.
i'm using this code:
func sendNotification()
{
var data = [String:Any]()
data["to"] = "a_device_push_token_from_a_real_device"
data["data"] = ["hello": "This is a Firebase Cloud Messaging Device Group Message!"]
data["notification"] = ["title":"test Device","body":"test body","sound":"default"]
let url = URL(string: "https://fcm.googleapis.com/fcm/send")
var request = URLRequest(url: url!)
request.setValue("key=firebase_server_key", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
and this code works, it manage to sent notification to device with "a_device_push_token_from_a_real_device"
My question is:How can i get the device token when i just have the User auth id ?
i triend this code :
func getNotificationKey(forUserId userId:String)
{
var data = [String:Any]()
data["operation"] = "create"
data["notification_key_name"] = "My Name"
data["registration_ids"] = [userId]
let url = URL(string:"https://android.googleapis.com/gcm/notification")
var request = URLRequest(url: url!)
request.setValue("key=firebase_server_key", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("firebase_project_id", forHTTPHeaderField: "project_id")
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: data, options: .prettyPrinted)
} catch let error {
print(error.localizedDescription)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString)")
}
task.resume()
}
but i receive :"{\"error\":\"no valid registration ids\"}") but i have a user in
Authentication
table with given id.
Any suggestion ?
and i know what is the difference between the firebase cloud message token, and Registration token is not a duplicate with this question.
I asked if is there any connection between them ? and how can i get all FCM tokens for a specific Registration Id ?