My app uses FCM legacy API to send notifications. But those APIs were deprecated on June 20, 2023, and will be removed in June 2024. In FCM Legacy Api I have used the following code to send notifications
` private void sendPushNotification(String notificationTitle, String hisFcmToken, String notificationBody, String myUserID, String notificationPhotoUrl, String hisUserID) {
try {
JSONObject jsonObject = new JSONObject();
JSONObject notificationObj = new JSONObject();
notificationObj.put("title", notificationTitle);
notificationObj.put("body", notificationBody);
notificationObj.put("image", notificationPhotoUrl);
notificationObj.put("priority", "high");
JSONObject dataObj = new JSONObject();
dataObj.put("isNewUncheckedLikeNotification", "true");
dataObj.put("likerUserID", myUserID);
jsonObject.put("notification", notificationObj);
jsonObject.put("data", dataObj);
jsonObject.put("to", hisFcmToken);
callApi(jsonObject, hisUserID);
} catch (Exception e) {
Log.d("sendLikePushNotification", "Exception: " + e.getMessage());
}
}
private void callApi(JSONObject jsonObject, String hisUserID) {
MediaType JSON = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String url = "https://fcm.googleapis.com/fcm/send";
RequestBody body = RequestBody.create(jsonObject.toString(), JSON);
Request request = new Request.Builder().url(url).post(body).header("Authorization", "Bearer Authorization_key").build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
if (response.isSuccessful()) {
Log.d("PushNotification", "Notification successfully sent.");
} else {
Log.d("PushNotification", "Failed to send notification: " + response.code() + " " + response.message());
}
}
});
}`
I have read this documentation but unable to understand clearly. I need an example code snippet or tutorial where the send notification is implemented completely. Thanks anyways.