0

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 ?

Community
  • 1
  • 1
Constantin Saulenco
  • 2,353
  • 1
  • 22
  • 44
  • 1
    You cannot send a FCM message to a UID. See my answer here: http://stackoverflow.com/questions/39157236/firebase-cloud-messaging-auth-tokens-vs-registration-token/39157528#39157528. You could map each user to a topic, as I've done in this blog post: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html – Frank van Puffelen Jan 16 '17 at 16:06
  • i know that i can't send a FCM message to a UID, that is why i asked if there is any way (any connection) to get the push tokens for a specific user Id from firebase authentication table – Constantin Saulenco Jan 16 '17 at 16:39
  • There is no automatic connection between a Firebase Authentication user and the Instance ID tokens of the app installs for that user. – Frank van Puffelen Jan 16 '17 at 17:10
  • but thanks to your blog, a manage to make push from device to device for iOS :) – Constantin Saulenco Jan 16 '17 at 17:30
  • 1
    Good to hear. Keep in mind that topics are public by nature: everyone can subscribe to them. So don't use them to relay sensitive information. – Frank van Puffelen Jan 16 '17 at 17:33

0 Answers0