Skip to content

Instantly share code, notes, and snippets.

@Ahmedbadereldin
Created September 4, 2020 11:10
Show Gist options
  • Save Ahmedbadereldin/3b41385b7c8223743767a097be509ab7 to your computer and use it in GitHub Desktop.
Save Ahmedbadereldin/3b41385b7c8223743767a097be509ab7 to your computer and use it in GitHub Desktop.
final private String FCM_API = "https://fcm.googleapis.com/fcm/send";
final private String serverKey = "key=" + "AAAA.....";
final private String contentType = "application/json";
String TOPIC;
////////////
TOPIC = "/topics/user_" + messageSendToUser.getId(); //topic has to match what the receiver subscribed to
JSONObject notification = new JSONObject();
JSONObject notifcationBody = new JSONObject();
try {
notifcationBody.put("title", "Message from ahmed bader el din");
notifcationBody.put("chatID", roomID);
notifcationBody.put("type", "chat");
notifcationBody.put("body", "Hi How Are You!!");
notification.put("to", TOPIC);
notification.put("notification", notifcationBody);
notification.put("data", notifcationBody);
} catch (JSONException e) {
Log.e("onCreate", "onCreate: " + e.getMessage());
}
sendNotification(notification);
////////////
private void sendNotification(JSONObject notification) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(FCM_API, notification,
response -> {
Log.i("onResponse", "onResponse: " + response.toString());
},
error -> {
Log.i("onErrorResponse", "onErrorResponse: Didn't work");
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
params.put("Authorization", serverKey);
params.put("Content-Type", contentType);
return params;
}
};
VolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjectRequest);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment