LAURENCE MORONEY: What's up, everybody?
This is Laurence from the Firebase team.
And welcome to another episode of Firecasts.
On today's episode, I'm going to show you
how you can send and receive messages using Firebase Cloud
Messaging on Android.
You'll be able to be up and running
with this feature in just a few minutes.
So let's get started.
The first thing you'll need to do
is to make sure that you have your app ready for Firebase.
You're probably familiar already with how to integrate Firebase
into your app, so I'm just going to go straight
to getting the ID token that you'll
need for Firebase Messaging.
This token is used to identify your device by the Cloud
Messaging system.
The code in my main activity, simply
called FirebaseInstance ID.getInstance.getToken,
to get the token.
I'll pass this token to my app server,
and the app server will then tell
FCM to send messages to the device identified
by this token.
Call FCM in this case is in a sync class
that is used to post the contents to the app server.
You'll see that in the next step.
Now, when building applications that use Firebase Cloud
Messaging, you'll need your own app server
to handle this messaging.
So I'm going to build a simple one in PHP next.
Here, you can see the app server that I created.
It's just running on a basic website.
And as you can see, I can send a message
to all devices or each one individually.
But how does this work?
In the previous step, you saw how
the Android app posted the registration ID
to the app server.
In this case, I wrote a simple app server
that just gets the Firebase registration IDs
and stores them in a database.
This database is then used to generate the page.
When the user sends to a particular device,
it uses its ID.
And if you want to send to all, you just
loop through all of them.
And here's the code.
Google APIs offers the send function
and you simply call this, passing it your authorization
key.
You pass it also the list of IDs that you
want to send the message to, which is the reg data
variable, and the message itself,
which is the data variable.
You might want to know how to get that authorization key.
Well, that's pretty simple.
It's available in the Firebase console.
Simply go to your project.
And in your app, manage your settings.
There's a Cloud Messaging tab, and the key is in it.
Back in the code-- as I'm using PHP,
I'm just going to exec this in [INAUDIBLE].
This function accepts the reg ID of the device
to send the message to and assigns that
to the registration ID's variable
that FCM.GoogleAPIs.com will understand.
This can be an array if you're sending to more than one.
The data field is just the message to send.
And it's as simple as that.
Now that you have a server, let's look
at building a simple client to receive messages.
And of course, you can send messages from this client, too.
Earlier, we saw that we created an app that registered
with Firebase Cloud Messaging.
And you can see that app here.
When I register, I see the device
listed on the app server's page by its ID.
So if I now type a message and send it,
let's see what happens.
I get the notification in my app.
So how does this work?
Well first, in my Android manifest .xml,
I'll define a service that receives an intent filter from
the Firebase Messaging event.
This class extends the Firebase Messaging service.
You can see this in the header.
And in its on message received, I
can capture the remote message.
In this case, I turn it into a notification
in the standard Android way.
This can take an intent so that I can open a new activity when
I click on the notification.
And you can see that here.
So when I touch on the notification,
this activity will be launched.
Let's see it in action.
And here, you can see I've received the notification.
When I touch on it, the activity launches with the message
from the notification metadata.
And if I go back to the website and find
this registered device, you can see the message I sent.
If I send another one containing the text's second notification,
you'll see that the device receives it.
And if I touch on it, the activity
will launch with that text.
And that's it for this Firecast.
Please leave your questions in the comments below or reach out
to me on Twitter or G+.
We'll be dropping new Firecasts every week,
so don't forget to hit that Subscribe button.
I'm Laurence Moroney, and thanks for watching.