Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to using FCM notifications for Android Application at https://firebase.google.com/docs/cloud-messaging/server. I was reading up on it and found that in the About FCM Server page requirements, it says the following:

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 appropriate XMPP or HTTP protocol

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

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

If I invoke this API using something like OkHttpClient and build my request like so, (provided that I have authentication headers and a POST body included)

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();
    }

}

Would I in theory, be able to send a notification with whatever information I want to that device? I can receive the 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’m sure my understanding is incorrect somewhere as the documentation does say we need an application server, but if someone could please point out where I am misunderstanding how to implement FCM notifications, that would be great. If someone could give an example of where or when we would need an app server, or how it should ACTUALLY be implemented, that would also be much appreciated. Thanks!

like image 955
user1871869 Avatar asked Aug 26 '16 08:08

user1871869


People also ask

What is the difference between Firebase Cloud Messaging and in-app messaging?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. Firebase In-App Messaging: Engage active app users with contextual messages. They both send messages to the app.

When should I use Firebase Cloud Messaging?

Using FCM, you can notify a client app that new email or other data is available to sync. You can send notification messages to drive user re-engagement and retention. For use cases such as instant messaging, a message can transfer a payload of up to 4000 bytes to a client app.

Which of the following can Firebase in-app messaging be used for?

Firebase In-App Messaging helps you engage your app's active users by sending them targeted, contextual messages that encourage them to use key app features. For example, you could send an in-app message to get users to subscribe, watch a video, complete a level, or buy an item.


2 Answers

When the documentation says that you need an app server is mainly because you need an application that store the tokens of the devices to which you would like to send the notifications and this application should update the tokens if any of your client devices change its token. However, you could use the OkHttpClient to send request to the FCM service and therefore send notification to other devices if you have, off course, the token ID of those devices. It depends on what you want to do and it depends on how you want to manage the notifications.

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

like image 144
GeorgeLBA Avatar answered Oct 14 '22 11:10

GeorgeLBA


If you use the XMPP protocol. You should implement a connection server that manages the connection to FCM to handle upstream and downstream messages.

This is a sample java project to showcase the Firebase Cloud Messaging (FCM) XMPP Connection Server. This project is a very simple standalone server that I developed as a base of a larger project. It is an application server that we must implement in our environment. This server sends data to a client app via the FCM CCS Server using the XMPP protocol.

https://github.com/carlosCharz/fcmxmppserver

And also I've created a video in youtube where I explain what it does.

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

Hope you find it useful.

like image 31
Carlos Becerra Rodriguez Avatar answered Oct 14 '22 09:10

Carlos Becerra Rodriguez