FCM Push Notifications: Java Server Side Integration

A step-by-step guide to implement FCM notifications in back-end Java application.


Firebase Auth Providers
Image Credit: Google.com

Firebase is a mobile application development toolkit created by the Google that helps the programmers quickly develop high-quality mobile apps, grow the user base, and earn extra cash by displaying ads via AdMob. It has everything you need to get started, from build, maintain to scale a mobile app on all major platforms like Android, iOS, Windows etc.

FCM Push Notifications: PHP Server Side Integration
A step-by-step guide to implement FCM notifications in back-end PHP application.

This is the second part of tutorial on Firebase Cloud Messaging (FCM) integration with Android application. In first part, we discussed FCM integration with Android app that consists of following four steps:

  1. Prerequisites
  2. Add Firebase to your Android project
  3. Setup FCM SDK in your project
  4. Access Device Registration Token

In order to proceed with FCM server side integration, I recommend completing the first part if you’ve done it yet. Let’s talk about our last step to send push notification from our server to any client device.

About FCM Server

The server side of FCM consists of two main components:

  • FCM connection servers provided by Google. These servers take messages from an app server and send them to a client app running on a device. Google provides connection servers for HTTP and XMPP.
  • An app server that you must implement in your environment. This app server sends data to a client app via the chosen FCM connection server, using the appropriate XMPP or HTTP protocol.

A full FCM implementation requires both a client implementation and a server implementation to work perfectly.

App Server Implementation in Java

Following Java code snippet does the job of sending a push notification from app server to a client device:

// Method to send Notifications from server to client end.

public final static String AUTH_KEY_FCM = "xyxyxyxxyxyxyxyxyxyxyxy";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

// userDeviceIdKey is the device id you will query from your database

public static void pushFCMNotification(String userDeviceIdKey) 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.trim());
   JSONObject info = new JSONObject();
   info.put("title", "Notificatoin Title"); // Notification title
   info.put("body", "Hello Test notification"); // Notification body
   json.put("notification", info);

   OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
   wr.write(json.toString());
   wr.flush();
   conn.getInputStream();
}

Send Message to Device Groups

Sending messages to a device group is very similar to sending messages to an individual device. Set the to parameter to the unique notification key for the device group.

json.put("to", "aUniqueKey"); //replace userDeviceIdKey with the unique notification key for the group

This completes the Android Push Notifications using Firebase tutorial. I hope, it has helped you to setup a basic understanding of what FCM is and how it is integrated in mobile apps.

Continue Reading: FCM Push Notifications: Receive Messages in an Andriod App

Need further help? If you’re confused or facing any issue while completing first step, let’s know in comments.

Love What You're Reading?
Subscribe to our top stories
Subscribe
Give it a try, you can unsubscribe anytime.
attacomsian
A passionate programmer, domainer, writer and photographer! Love writing about web development, latest trends in technology and domain name industry. Working hard to make the world a better place! Follow me at Twitter @attacomsian or visit my website www.attacomsian.com

FCM Push Notifications: Java Server Side Integration

log in

Become a part of our community!

reset password

Back to
log in
DON’T MISS OUT!
Subscribe To Our Newsletter
Be the first to get latest updates and exclusive content straight to your email inbox.
STAY UPDATED
Give it a try, you can unsubscribe anytime.
close-link
Love What You're Reading?
Subscribe to our top stories
Subscribe
Give it a try, you can unsubscribe anytime.
50% Off on all advertisement packages. Limited time only!
ORDER NOW