When do you need an application server for Firebase Cloud Messaging?

I am new to using FCM notifications for an Android app at https://firebase.google.com/docs/cloud-messaging/server . I read about it and found that the requirements for the About FCM server page say the following:

An application server that you must implement in your environment. This server application sends data to the client application through the selected FCM server connection using the appropriate XMPP or HTTP protocol.

However, I am very confused about this. As I read more in the article, I see that there is an API that looks like this:

POST http://fcm.googleapis.com/fcm/send

If I call this API using something like OkHttpClient and build my request like this (assuming I have authentication headers and a POST body)

 private void sendRegistrationToServer(String token) { OkHttpClient client = new OkHttpClient(); RequestBody body = new FormBody.Builder().add("Body", "").build(); //Assuming I have authentication and body put in Request request = new Request.Builder().url("http://fcm.googleapis.com/fcm/send").post(body).build(); try { client.newCall(request).execute(); } catch (IOException e) { e.printStackTrace(); } } 

Will I, in theory, be able to send a notification with any information I want on this device? I can receive a message through the following class:

 public class NotificationService extends FirebaseMessagingService { ... // [START receive_message] @Override public void onMessageReceived(RemoteMessage remoteMessage) { // TODO(developer): Handle FCM messages here. Log.d(TAG, "From: " + remoteMessage.getFrom()); // Check if message contains a data payload. if (remoteMessage.getData().size() > 0) { Log.d(TAG, "Message data payload: " + remoteMessage.getData()); } // Check if message contains a notification payload. if (remoteMessage.getNotification() != null) { Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); } } 

I am sure that my understanding is incorrect somewhere, as the documentation says that we need an application server, but if someone could indicate where I do not understand how to implement FCM notifications, that would be great. If someone could give an example of where or when we need an application server, or how it should actually be implemented, this would also be highly appreciated. Thanks!

+6
source share
3 answers

When the documentation says that you need an application server, mainly because you need an application that stores device tokens to which you want to send notifications, and this application should update tokens if any of your client devices change it token. However, you can use OkHttpClient to send a request to the FCM service and, therefore, send notifications to other devices, if you have, of course, the token identifier of these devices. It depends on what you want to do, and it depends on how you want to manage notifications.

If you need an example of how to implement a server application in java, this is a good example of example 1 that was published, or here is another post with a PHP implementation. If you need an example on how to implement a client application and how to test it from the firebase console, here is another good example .

+1
source

If you are using XMPP. You must implement a connection server that manages the connection to FCM to handle upstream and downstream messages.

This is an example java project to demonstrate the Firebase Cloud Messaging (FCM) XMPP Connection Server. This project is a very simple stand-alone server, which I developed as the basis of a larger project. This is an application server that we must implement in our environment. This server sends data to the client application through FCM CCS Server using the XMPP protocol.

https://github.com/carlosCharz/fcmxmppserver

And I also created a video on YouTube where I explain what it does.

https://www.youtube.com/watch?v=PA91bVq5sHw

I hope you find this helpful.

+1
source

No need to do extra work, just follow the links below:

https://github.com/firebase/quickstart-android/tree/master/messaging

0
source

All Articles