I am new in Android development and I am trying to work on a project which involves an application that uses Firebase database. I want to send notifications to users when something happens, i.e. when I assign a task to a user.
I have implemented necessary Firebase classes and I can successfully send notifications to users, using their token from Firebase console. However, I can't manage to send custom notifications from the code in Android Studio.
I have tried the following function and I am passing the token of a device plus a title and a body for the notification and it's not working.
public static void pushFCMNotification(String userDeviceIdKey, String title, String body) throws Exception{
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",userDeviceIdKey);
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
Can someone give me some guidelines or any other ways of implementing such notifications?