To receive simple downstream messages, each client app needs to implement the
methods on the
firebase::messaging::Listener
API.
Initialize FCM
Before you can use FCM to get access to your registration token or receive messages it must be initialized.
To initialize FCM, call
::firebase::messaging::Initialize
and supply it with your
::firebase::App
object as well as an implemention of the
::firebase::messaging::Listener
class.
MyListener my_listener_implementation; ::firebase::messaging::Initialize(app, &my_listener_implementation);
Access the registration token
On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices, or create device groups for FCM, you'll need to access this token.
You can access the token's value through the
::firebase::messaging::Listener::OnTokenRefresh
virtual function.
void OnTokenRefresh(const char* token) {
LogMessage("The registration token is `%s`", token);
// TODO: If necessary send token to application server.
}
Receive and handle messages
To receive messages, your Listener class must implement the
OnMessage
virtual function.
Override OnMessage
By overriding the method
::firebase::messaging::Listener::OnMessage,
you can perform actions based on the received message and get the message data:
void OnMessage(const ::firebase::messaging::Message& message) {
LogMessage(TAG, "From: %s", message.from.c_str());
LogMessage(TAG, "Message ID: %s", message.message_id.c_str());
}

