Firebase Cloud Messaging (notifications)

library link: https://github.com/karanatwal/FirePush

Reference link: https://stackoverflow.com/questions/37576705/firebase-java-server-to-send-push-notification-to-all-devices/42455788

official link: https://firebase.google.com/docs/cloud-messaging/send-message

App Level Build.Gradle :

implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-messaging:20.0.0'
step 1: create a service class that extends firebaseMessagingService class:
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
         //get default notificaion data
         String mesgeTitle=remoteMessage.getNotification().getTitle();
         //get extra data
         String msgType=remoteMessage.getData().get("msg_type");
}

}
step 2: enable this service into manifest file
inside : <application></application>

<service android:name=".FirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>
step 3: generate Token: override inside firebaseMessagingService.class 
@Override
 public void onNewToken(String token) {
     sendRegistrationToServer(token);
 }

step 4: post request to send notification

package com.m27lab.myapp.FirebaseCloudMessaging;

import android.os.AsyncTask;
import android.os.StrictMode;

import org.json.JSONObject;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

public class MySend {
    public static void sendNotification(final String token,final String title, final String Message)
    {
        String server_key ="AAAAb3e2fgg:YOUR-SERVER-KEY from firebase cloud messaging" ;

        AsyncTask.execute(() -> {
            int SDK_INT = android.os.Build.VERSION.SDK_INT;
            if (SDK_INT > 8) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                        .permitAll().build();
                StrictMode.setThreadPolicy(policy);

                //This is a Simple Logic to Send Notification different Device Programmatically....

                try {
                    String jsonResponse;

                    URL url = new URL("https://fcm.googleapis.com/fcm/send");
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    con.setUseCaches(false);
                    con.setDoOutput(true);
                    con.setDoInput(true);

                    con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
                    con.setRequestProperty("Authorization", "key="+server_key);
                    con.setRequestMethod("POST");

                    JSONObject notification = new JSONObject();
                    notification.put("title",title);
                    notification.put("body",Message);

                    JSONObject Nbody = new JSONObject();
                    Nbody.put("to",token);
                    Nbody.put("notification", notification);

                    String strJsonBody=Nbody.toString();


                    System.out.println("strJsonBody:\n" + strJsonBody);

                    byte[] sendBytes = strJsonBody.getBytes("UTF-8");
                    con.setFixedLengthStreamingMode(sendBytes.length);

                    OutputStream outputStream = con.getOutputStream();
                    outputStream.write(sendBytes);

                    int httpResponse = con.getResponseCode();
                    System.out.println("httpResponse: " + httpResponse);

                    if (httpResponse >= HttpURLConnection.HTTP_OK
                            && httpResponse < HttpURLConnection.HTTP_BAD_REQUEST) {
                        Scanner scanner = new Scanner(con.getInputStream(), "UTF-8");
                        jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
                        scanner.close();
                    } else {
                        Scanner scanner = new Scanner(con.getErrorStream(), "UTF-8");
                        jsonResponse = scanner.useDelimiter("\\A").hasNext() ? scanner.next() : "";
                        scanner.close();
                    }
                    System.out.println("jsonResponse:\n" + jsonResponse);

                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }
        });
    }
}

Author: joker