Przejdź do głównej zawartości

Sending Firebase Messages from command line with curl

Google Firebase platform is set of services aimed at "cloudyfying" Android apps. One of those services is FCM, Firebase Cloud Messaging, and allows us to send short text or data messages to device or group of them.
Majority of tutorials show how to send this message from another Android device. This one will show how to send it from custom server, for simplicity using curl program. 
Steps needed:
  1. Set up project in Firebase console
  2. Compile example app for this project
  3. Verify sending works with web ui
  4. Send message using curl

Set up project in Firebase console

  1. Go to
  2. Click Add project
  3. Enter project name and click Create
  4. On the left menu select Grow / Notification
  5. Click Android icon 
  6. Enter package name. For this tutorial enter
  7. Click Register App, then Download google-services.json and save the file
  8. Follow rest of instructions on website or in this tutorial
2. Adding new project
3. Project needs a name
6. Package name of android app
7. Download google-services.json
App's screen

Compile example app

  1. Check out project firebase/quickstart-android
    git clone
  2. put downloaded gooogle-services.json file into
  3. Open in android-studio messaging app (NOT the whole quickstart-android)
  4. Compile and run the app on device, with adb logs visible
    At least use $ adb logcat | grep "InstanceID Token"
  5. Run app Firebase Cloud Messaging
  6. Click LOG TOKEN. This is needed for single device messages
  7. Read registration token from adb logcat. It should look like this:
    01-17 08:44:22.933 15548 15548 D MainActivity: InstanceID Token: <registration_token>
  8. Save the registration token, it will be needed to send message.
  9. Click SUBSCRIBE TO NEWS. This is needed for group messages

Verify sending works with web ui

  1. Go to firebase console, Grow / Notification
  2. The first time you see welcome screen
  3. Fill necessary fields
    1. Message text - anything; this will be sent to device
    2. Target - select Single device
    3. FCM registration token - long string that you obtained from logcat when running app
  4. Click Send Message and Send again
  5. Check your phone; make sure it is connected to Internet (either 3g or WiFi will do)
  6. Clicking on notification should open the sample app
2. Nice welcome screen
3. Preparing the message
There are three alternatives when specifying where to send the message. 
  • User segment - can be used to target all users of the app or some subset of them (for example users of specific language).
  • Topic - users that subscribed to specific topic. For example news.
  • Single device - identified by token.

Send message using curl 

  1. Go to firebase console
  2. Click gear icon in top left corner, select Project Settings
  3. Select tab Cloud Messaging
  4. Copy Server Key value
    • Shorter Legacy Server Key would also work, but to be safe use longer one
  5. Go to bash console, enter command
    curl -X POST \
    --header "Authorization: key=<server_key>" \
    --header "Content-Type: application/json" \ \
    -d '{"to":"<registration_token>","notification":{"body":"ENTER YOUR MESSAGE HERE"}'
    remembering to substitute <server_key> and <registration_token&gt with proper values.


Steps 1-4 are necessary only once to get server key.
Curl program is used to send messages in several supported protocols, HTTP among them. We need POST method (-X POST) of HTTP protocol (default).

--header parameters add HTTP headers. They are distinct from message body, but necessary. Authorization is checked by Firebase server to check whether we are allowed to send messages to given recipient. Content-type clarifies how message data is organized.
The url is address of Google server that will handle the request. https is not optional.
-d '....' is message data to send. It is json text. I used apostrophes so that quotes in json don't need to be escaped.


So after those steps you are able to send Firebase Push messages to Android device from any program that has access to curl (or libcurl library). Of course you don't have to use curl and in your project you can use any library/framework you want. Keep in mind that it needs to be able to create ssl connection for https.


Popularne posty z tego bloga

Cleaning up #includes

Cleaning up includes In previous article I described why you should care about clean #includes and what rules I suggest to achieve it. If you already have a project that doesn't follow those rules, it would be tedious manual task to implement my advises. This article describes how some tools can help you answer these questions:
How to replace include guards with pragma once in whole project?How to sort includes in whole project?How to remove redundant includes and add missing ones?How to replace angle bracket includes with quotes only for project headers? #pragma once with include-guard-convert.pyInclude-guard-convert is simple tool that will find all include guards and change them to #pragma once. Tool doesn't need installation, simply download script from github and run it on your header files:
wget chmod +x ./ proj_path/**/*.…

GDB - beyond basics

GDB is console debugger that every Linux-using programmer heard about. It is not however easy to learn. Greg Law in his CppCon talk presented some of obscure, but useful features.

Text user interface Normally we use GDB with command line interface (CI). Beyond this, GDB has TUI based on Curses library. To activate it, use keyboard shortcut ctrl-x-a (hold ctrl, press x, unpress x, press a). Now you can see the code as you go through it.
ctrl-x-a - activate/deactivate TUIctrl-l - when screen gets messed up, use it to redraw. Happens when program prints to stdout/stderrctrl-p / ctrl-n - since you can't use arrows to reuse previously written command, use ctr-p/n instead of arrow up/down
ctrl-f / ctrl-n are arrows left / right
ctrl-a / ctrl-e are home / end (all those are copied from Emacs)ctrl-x-2 - second window (assembly). Shell You can run shell commands inside GDB command line. Just use keyword "shell" at the beginning. Examples:
shell psshell cat temporary_file.txtshell k…

Tidy #includes

Tidy #includes Having tidy includes in C++ project is often thought as low priority. It can however help you with making your code less fragile. This is doubly important when your project is a library that will be used by other people.
I propose that "good" includes should follow those rules:
Associated header (foo.hpp for foo.cpp) always comes firstAny file should have all the headers it needs. System headers are in <angle_brackets>, project headers are in "quotes"Project headers are first, STL headers are last, between them are libraries in order of decreasing locality.Use #pagma once instead of include guards Within groups decided by 4), alphabetic order is nice to have.  Associated header is first The header that declares class / methods should be the first to include. It also should have the same file name as current source file, except for extension. Some tools depend on this - for example IDEs use this to implement "go to declaration/implementatio…