JEN PERSON: Hey, all you GCM developers.
It's time to become Firebase Developers.
I'm Jen Person, and today I'll show you
how to migrate from GCM to FCM.
Google Cloud Messaging, or GCM, has
been a reliable solution for sending
billions of messages a day.
That's right, billions.
Firebase Cloud Messaging is the new version of GCM.
It inherits the reliable and scalable GCM infrastructure
plus new features.
Now, if you're a developer currently using GCM,
it can be daunting to think about migrating to FCM,
especially when you've already spent time creating
a great messaging solution.
But I'll show you today that it takes a little time and effort
to get your Android app up to speed.
And as Cloud Messaging continues to grow and improve,
new features will only be available on Firebase Cloud
Messaging.
So why not migrate today?
Follow along as I migrate an app from GCM to FCM.
Here's my app that I'm going to migrate.
It displays a schedule of sportsball games going
on each week, as well as scores from previous games.
On the back end, it uses an HTTP-triggered function
with a cron job that is run once a day
to send a message to users reminding them about the games
to watch that evening and summarizing
the previous night's games.
Here's what the body of the function looks like right now.
My function creates an HTTP post request,
sending a chosen message to the given GCM token.
The message is usually sportsball scores or schedules,
which come from a database of sportsball trivia.
Now, if you've seen the guide for migrating from GCM to FCM,
it may look like there's a lot to do.
But when you break it down, it's much more manageable.
The migration process as I see it is threefold.
Console side, client side, and server side.
So we're going to tackle these one at a time.
Now as a side note, if you're using this video
to help with the migration to FCM,
I recommend you have the migration guide open
at the same time, so you can copy code snippets as needed.
This will be much easier than trying
to transcribe the code I'll show you on screen.
You can find a link to the guide below.
OK, let's get started.
First, console side.
In the Firebase console, I'm going to select Import Google
Project.
I'll select my sportsball scores from my list of projects
and select Add Firebase.
In the Firebase welcome screen, I
choose Add Firebase to your Android app.
I'll provide the package name and the SHA-1 of my app.
And then I'll add the app.
This downloads a new Google Services JSON file,
which I'll add to the app.
And that's all I need to do console side to get my app up
to speed.
OK, with the console ready to go,
let's tackle the client-side changes.
This is where we'll be making the bulk of our changes.
Well, let's just dive in.
First, I import the Google Services JSON
I just downloaded.
Then I'll change the dependency from GCM
to FCM in my apps build.gretl file.
Next, I'll remove permissions required by GCM.
All permissions required by FCM are added automatically.
Pretty cool.
I'll also remove the receiver from the app manifest.
Again, with FCM, the broadcast receiver
is added automatically.
All right, get this.
Those are the main code changes required
to use FCM in my Android app.
That's it.
From here, I can just remove any other code that
was once required to use GCM.
But there are several features of Cloud Messaging
your app may be using that will require some changes,
rather than just removing all the code.
I'll show you how to make these changes.
With my app, Sportsball Scores, I currently
use an extension of Instance ID Listener Service.
Now, with FCM, this extension is only
needed if I want to access the FCM token.
And I need this token if I want to, let's say,
manage tokens from a server so I can send directly
to a device or to subscribe devices
to topics from a server.
In my case, I do want to be able to send to specific devices.
So I'll want to extend this service.
But you may not have to, depending
on the needs of your app.
But I'll show you what that looks like now.
I'll update the Instance ID Listener Service
code in my apps, Android manifest, to use FCM.
And now I'll update My Instance ID Listener Service like this.
I no longer need to explicitly initiate the generation
of a registration token.
That FCM library does this automatically for me.
So I can just remove this code.
OK, let's take a look at what else we might have to change.
A service extending the messaging listener service
is now required only if your app receives messages
with a notification payload while the app is
in the foreground, receives messages
with a data payload only, or receives errors
in case of the upstream message failures.
Meaning, if you don't use any of these features,
you can completely remove this service.
I'm going to include this extension so that users
can get updates from Sportsball Scores when the app is open.
So let's look at that now.
So I'll make the necessary changes to my Android manifest.
And update my GCM Listener Service
to My FCM Listener Service, extending Firebase Messaging
Service.
All right, we're almost done.
There's one more change that you will want
to make to your client app.
If your app uses GCM Pub/Sub to send messages to topics,
you'll want to update this code.
As a side note, for users who are subscribed
to topics on GCM, I recommend you subscribe them
to those topics on FCM.
While GCM tokens topic subscriptions
will continue to work in most cases,
subscribing FCM tokens will help to avoid any edge cases.
And subscribing the FCM tokens will not
generate duplicate messages, so you
don't have to worry about accidentally
spamming your users with double messages.
I want users to be able to subscribe to sportsball score
notifications for their favorite teams,
so I'll include this in my app.
And when I update, I can take advantage
of some features of FCM.
First of all, FCM subscribe to topic method
doesn't block the current thread.
The operation is automatically performed in a background
thread, meaning you won't need to manage threads
as you did with GCM.
Second of all, the operation is stored
and automatically retried in the case of connectivity issues.
Finally, the token that was required with GCM
is now automatically created in FCM
using the sender ID specified by the Firebase project.
And the changes I'll make in my app
are just a couple of lines of code.
Here, I'll show you.
In my GCM Pop/Sub function, I'll change my functions
for subscribing to topics.
With this function, San Francisco sportsball fans
can subscribe to updates about their team.
All right, now the console is set up
and my client app is ready to go.
The final changes I need to make are server side.
I'll need to update to a new, more secure server key, which
I can get from the Firebase console,
and change my HTTP call to use the FCM endpoint.
Let's do it.
In the Firebase console, under Project Settings,
Cloud Messaging, I'm going to download the service account
credentials.
Then I'll drag the JSON file into my project.
This may seem like extra work, but it's
worth it to get the enhanced security that service account
credentials provide.
Since the access tokens are short-lived,
lasting about an hour, the risk posed by a leaked access token
is much more limited.
Then I'll replace the GCM API key with this service account
key.
I'll update my HTTP header to use this access token.
I'll also update my HTTP endpoint to FCM
and then redeploy my function.
Now, I'll run my app, trigger the function,
and here you can see the latest sportsball scores.
Now that I've upgraded my app to use FCM,
I can take advantage of new features
like topic conditions and platform overrides.
I can create topic groups that send
team-specific notifications based on which
games the users care about.
There's so much more to do.
The content could fill several Firecasts.
In fact, that's not a bad idea.
Since you've migrated to FCM, be sure to subscribe
to the Firebase channel so you can find out when
there are new Firecasts on FCM.
And check out our other great videos on Firebase.
I'll see you on a future episode of Firecasts.