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

3

6

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!

Russ answered 26/8, 2016 at 8:42 Comment(7)
Yes you can send notification from device using Firebase APIReprehension
What error exactly your getting?Reprehension
firebaser here Sending downstream messages through an API requires the use of your project's server key. Since this key allows sending messages on your app's behalf, it should never be embedded in an app that you send to your users. Hence the need for an app server.Jay
@FrankvanPuffelen I see what you're saying. So in my application I have Firebase Database setup. Are you saying that I should hard-code the API key into my Firebase Database and then simply pull from the database to get the key? Why should I not embed it into my app as a const final String?Russ
No. That would be equally dangerous. You cannot send device to device messages with Firebase Cloud Messaging without using an app server. Exposing your server key to your clients is a bad idea. For one approach, see this blog post: firebase.googleblog.com/2016/08/…Jay
@FrankvanPuffelen Ah. I see. Why is exposing a server key to your client a bad idea? I'm not sure where to put my Node.js script in my Android project if I do it this way. Could you point me to a resource that could show me how to hook up Node.js with my Android App?Russ
The blog post explains why it's a bad idea to have your server key in the client app. It also shows how to use the Firebase Database for the communication between your app and the node.js server.Jay
T
1

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.

Trstram answered 26/8, 2016 at 13:26 Comment(3)
I see. Thanks for your help. One question actually: with an implementation on PHP, how can I call the PHP script form my Java code? How can I communicate with the PHP script so that it sends a notification with whatever information I would want?Russ
@Russ if you have your server implementation in PHP you can call the PHP script from Java using the standar libraries for making POST and GET request. For example as described in this example. So, you can send data from the java app to the PHP script so that the PHP script send the notification to the mobile devices using Firebase. This would be useful if your PHP server implementation stores the tokens of the mobile devices.Trstram
As I commented above to the question, sending downstream messages requires that you specify the project's server key, which should never be embedded in your app.Jay
H
1

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.

Hord answered 22/9, 2016 at 22:42 Comment(0)
U
-1

No need to do any extra work just follow below link:

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

Usurer answered 26/8, 2016 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.