Cloud Messaging with Firebase Admin SDK

Firebase just released a set of new Cloud Messaging APIs in the Admin SDK. This release brings the popular Firebase Cloud Messaging (FCM) support to Admin Java, Python and Go SDKs, while also giving a boost to the existing messaging capabilities of the Admin Node.js SDK. The FCM APIs in the Admin SDK enable developers to send messages from server-side environments to client applications running on mobile devices and web browsers. Firebase client SDKs for Android, iOS and Web provide the client-side APIs necessary to receive incoming messages.

FCM messages may contain data payloads that are consumed by client apps, notifications that are displayed to the end users, or both. Developers can send FCM messages to a specific app instance, or a topic that many clients can subscribe to. The former implements point-to-point messaging semantics, and is achieved by addressing messages to a registration token that uniquely identifies an app instance. The latter implements pub-sub messaging semantics, and is ideal for sending mass notifications at scale. Moreover, FCM messages can be addressed to a condition expression, which enables sending messages to clients who have subscribed to specific combinations of topics. Listing 1 shows how to send a notification from a Node.js application to all clients subscribed to both auto-news and green-earth topics.

Listing 1: Sending a message to a condition expression with Admin Node.js SDK

The new APIs in the Admin SDK are based on the FCM HTTP v1 API, which provides added security, more configuration options, and the flexibility to tailor a message to multiple target platforms. For example, a developer can specify configuration options for Android, iOS and Web in the same message as illustrated in the Java example in listing 2. The FCM service customizes the message content and presentation to each target platform at delivery.

Listing 2: Customizing a message for multiple platforms with Admin Java SDK

Admin SDK also provides a dry run mode that can be used to emulate sending FCM messages. This is a great way to check if a given message will be accepted by the FCM service for delivery. In the dry run mode, the Admin SDK and the FCM service perform all the usual validations, but do not actually deliver the messages to any clients. Listing 3 shows the dry run mode being used in a Go application.

Listing 3: Sending a message in the dry run mode with the Admin Go SDK

In addition to sending FCM messages, the Admin SDK also supports managing topic subscriptions via server-side code. Developers can subscribe client registration tokens to FCM topics, or unsubscribe them in bulk. Listing 4 shows a Python application retrieving a set of registration tokens from the Firebase database, and subscribing them to a topic named releases. The subscribe/unsubscribe operations in the Admin SDK can handle up to 1000 registration tokens at a time.

Listing 4: Subscribing registration tokens to a topic with Admin Python SDK

FCM support has been a frequently requested feature for Firebase Admin SDKs. Last week’s release is a major step towards fulfilling those requests, and many app developers should find it useful. Overall I think these server-side features make Firebase quite powerful and compelling as an app development platform.

The capabilities described in this post are now available in all four variants of the Admin SDK. Refer to the documentation for more details and code samples. I’d also like to hear about what you think of the new APIs, and how they can be improved. Firebase Admin SDKs are open source, and therefore you can help further develop them as well. In fact, a good chunk of the code for the FCM support in the Admin Go SDK came from a contributor in the community. Have fun with FCM, and happy coding with Firebase!

Hiranya Jayathilaka

Written by

Software engineer at Google. Enjoys working at the intersection of cloud, mobile and programming languages. Fan of all things tech and open source.