How to send push notifications from arduino to android via FCM?

Viewed 14

Im trying to send a notification to my device via HTTP request in arduino, i have use this code so far, but it doesnt work, i dont really know where to find "SERVER_KEY", i have seen that in the project configuration ->cloud messaging should be, but its not.configuration cloudmessaging, also i dont know if the code is going to work, can you help me?

void sendDataToFirebase() {
  String data = "{" ;
  data = data + "\"to\": \"DEVICE_TOKEN\"," ;
  data = data + "\"notification\": {" ;
  data = data + "\"body\": \"Hi\"," ;
  data = data + "\"title\" : \"Alarm\" " ;
  data = data + "} }" ;
 
  Serial.println("Send data...");
  if (client.connect("fcm.googleapis.com", 80)) {
    Serial.println("Connected to the server..");
    client.println("POST /fcm/send HTTP/1.1");
    client.println("Authorization: key=SERVER_KEY");
    client.println("Content-Type: application/json");
    client.println("Host: fcm.googleapis.com");
    client.print("Content-Length: ");
    client.println(data.length());
    client.print("\n");
    client.print(data);
  }
  Serial.println("Data sent...Reading response..");
  while (client.available()) {
   char c = client.read();
   Serial.print(c);
  }
  Serial.println("Finished!");
  client.flush();
  client.stop();
}
0 Answers
Related