I am trying to send a notification to a user but I am unsure if i am using correct approach as from running this code there is no new notifications in firebases console. I am able to pass the token of the destination and title and body however i am not sure if the post request is correct. Any advice would be great thanks.
public class ClientNotifications extends AsyncTask<String, Void, String> {
String token;
String titles;
String body;
public ClientNotifications(String token,String title,String body)
{
this.token = token;
this.titles=title;
this.body=body;
}
@Override
protected String doInBackground(String[] params) {
JsonObject jsonObj = new JsonObject();
// client registration key is sent as token in the message to FCM server
jsonObj.addProperty("token", token);
JsonObject notification = new JsonObject();
notification.addProperty("body", body);
notification.addProperty("title", titles);
jsonObj.add("notification", notification);
JsonObject message = new JsonObject();
message.add("message", jsonObj);
final MediaType mediaType = MediaType.parse("application/json");
OkHttpClient httpClient = new OkHttpClient();
try {
Request request = new Request.Builder().url("https://fcm.googleapis.com/v1/projects/yourfirebaseproject/messages:send")
.addHeader("Content-Type", "application/json; UTF-8")
.addHeader("Authorization", "Bearer " + "key")
.post(RequestBody.create(mediaType, message.toString())).build();
Response response = httpClient.newCall(request).execute();
if (response.isSuccessful()) {
}
} catch (IOException e) {
Log.e("HI",e.toString());
}
return message.toString();
}
@Override
protected void onPostExecute(String message) {
}
}