Recently I asked a question on sending push notifications using GCM: Send push notifications to Android. Now that there is FCM, I am wondering how different it would be from the server side development. Coding wise, are they the same? Where can I find example FCM codes showing sending push notifications from server to Android device?
Do I need to download any JAR library for sending notifications to FCM using Java codes? The example codes in Send push notifications to Android shows sending push notifications using GCM and a server side GCM JAR file is required.
However, another example in https://www.quora.com/How-do-I-make-a-post-request-to-a-GCM-server-in-Java-to-push-a-notification-to-the-client-app shows sending push notifications using GCM and no server side GCM JAR file is required since it is just sending via an HTTP connection. Can the same codes be used for FCM? The URL used is "https://android.googleapis.com/gcm/send". What would be the equivalent URL for FCM?
Thanks in advance.
Create a google API project. Enable push notifications for the project and get a API key. Get a registration ID through android app (each device has a registration ID for a specific application) Create a server application to send your push messages as push notifications through google servers by GSM.
Each mobile operating system provides its own solution for native application push notifications, so they can be tricky to integrate into your app. In Android, the mechanism native applications use for push notifications is Firebase Cloud Messaging (FCM) notifications.
How it is possible? It's definitely possible -- you don't have to use Firebase to deliver push notifications.
How different is server-side coding?
Since there is not much difference, you can just check out most of the example server-side codes for GCM as well. Main difference with regards to GCM and FCM is that when using FCM, you can use the new features with it (as mentioned in this answer). FCM also has a Console where you can send the Message/Notification from, without having your own app server.
NOTE: Creating your own app server is up to you. Just stating that you can send a message/notification via the console.
The URL used is "https://android.googleapis.com/gcm/send". What would be the equivalent URL for FCM?
The equivalent URL for FCM is https://fcm.googleapis.com/fcm/send. You can check out the this doc for more details.
Cheers! :D
Use below code to send push notification from FCM server :
public class PushNotifictionHelper { public final static String AUTH_KEY_FCM = "Your api key"; public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send"; public static String sendPushNotification(String deviceToken) throws IOException { String result = ""; URL url = new URL(API_URL_FCM); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM); conn.setRequestProperty("Content-Type", "application/json"); JSONObject json = new JSONObject(); json.put("to", deviceToken.trim()); JSONObject info = new JSONObject(); info.put("title", "notification title"); // Notification title info.put("body", "message body"); // Notification // body json.put("notification", info); try { OutputStreamWriter wr = new OutputStreamWriter( conn.getOutputStream()); wr.write(json.toString()); wr.flush(); BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } result = CommonConstants.SUCCESS; } catch (Exception e) { e.printStackTrace(); result = CommonConstants.FAILURE; } System.out.println("GCM Notification is sent successfully"); return result; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With