9 shares

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.


Like it? Share with your friends!

9 shares
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

146 Comments

Your email address will not be published.

  1. Pingback: ivermectin generic
  2. Pingback: deltasone function
  3. Pingback: durvet ivermectin
  4. Please let me know if you’re looking for a article author for
    your weblog. You have some really great posts and I think I would be a good asset.
    If you ever want to take some of the load off, I’d really like to write
    some material for your blog in exchange for a link back to mine.
    Please blast me an email if interested. Thanks! http://herreramedical.org/prednisone

  5. Pingback: Anonymous
  6. Pingback: Anonymous
  7. Pingback: Anonymous
  8. Pingback: ivermectin kaufen
  9. Pingback: cost of ivermectin
  10. Pingback: ivermectin 2ml
  11. Pingback: ivermectin 250ml
  12. Pingback: plant viagra
  13. Pingback: order furosemide
  14. Pingback: stromectol buy uk
  15. Pingback: tadalafil order
  16. Pingback: cialis mg
  17. Pingback: canadian tadalafil
  18. Pingback: cialis dosis
  19. Pingback: viagra wirkung
  20. Pingback: borgata free
  21. Pingback: covid ivermectin
  22. Pingback: cialis 20mg online
  23. Pingback: liquid viagra
  24. Pingback: ivermectin paste
  25. Pingback: furosemide 80 mg
  26. Pingback: buy xanax online
  27. Pingback: ambienonline
  28. Pingback: ivermectin 200
  29. Pingback: stromectol tablete
  30. Pingback: stromectol order
  31. Rexbet Canlı Bahis Rexbet Canlı Bahis menüsü de Türkiye’deki tüm bahisçileri 12’den vurmuş durumda. Öncelikle belirtmek isterim ki, sitenin canlı bahis ara yüzü neredeyse mükemmele yakın durumda. Burada canlı bahis oynamanın keyfini sonuna kadar hissedeceğine bahse girebilirim. Rexbet canlı bahis keyfi bir başka.

  32. Pingback: do-posle-psihologa
  33. Pingback: 3simpleton
  34. Pingback: confeitofilm
  35. Pingback: A片
  36. Hi, just required you to know I he added your site to my Google bookmarks due to your layout. But seriously, I believe your internet site has 1 in the freshest theme I??ve came across.