Sending Messages from App Server to Clients Using FCM
Having capability to send messages from app server to client apps helps in providing relevant updated information to users which in turn helps in improving user engagement.
Firebase cloud messaging service provides a platform for server side applications to build a push notification feature that can send messages to client applications such as android and iOS applications.
In this post, I’ll explain about types of messages that can be sent to FCM server, how to send single device message, how to send messages to multiple devices, server app set up and sending messages and client app set up and handling messaging on the clients.
FCM supports three types of messages, notification message, data message and message containing both notification and data. Notification message can be used to send just notification which is displayed on the device notification bar. If data needs to be sent to client app as part of message, data message is useful.
Firebase SDK on the client app processes the notification messages automatically and displays on the notification bar when app is in back ground. To handled data messages when app is in background or foreground and notification messages when app is in foreground, client needs to have a component that is created using Firebase could messaging API .
Using Firebase cloud messaging service, servers can send messages to multiple devices or a specific device. To send a message to multiple clients, FCM topic is used. App server publishes notification to a FCM topic and FCM server sends the notification to subscribed client apps. To learn about how to send messages to multiple client, please read sending push notifications to multiple clients from app server using firebase cloud messaging and FCM topic with example.
To send a message to a specific client, registration token generated by Firebase SDK on the client is required. Client needs to send the token to app server and app server includes the token in the message so that FCM server can deliver the message to the target client app. To know how to capture and send Firebase generate token from client to app server and how app server uses it to send message, you can read sending device specific message from app server to FCM server and handling the message on client with example.
App server needs to use private key generated for a service account in Firebase console to generate access token. The access token authenticates and authorizes the server app to use Firebase services. The token needs to be included in the header of the request. You can refer to Firebase cloud messaging app server tutorial.
You need to configure your client app project to make it process FCM messages. Client app needs to install Firebase SDK, include FCM libraries and create components that handle messages. To know how to handle messages on android, you can read Firebase cloud messaging android example.