Firebase Cloud Messaging — Firebase Quickstart for Android

tahaburak.koc
4 min readDec 19, 2020

Hello, I’d like to introduce Firebase Quickstart for Android. It helps a lot to learn about Firebase and the implementation of its features. (Also you never know when you’ll need an Android app that you’d like to send push notifications to publish an article series on Medium. Yep, that’s the reason beneath.)

Logo

0. Prerequisites

At this point having access to a Google account and Android Studio is pretty much all you need. (And that’s what makes it quickstart, right?)

1. Importing the code sample

Just fire up(not so p-unny, huh?) the Android Studio and press this lovely button.

The sample we looking for is “Firebase Quickstarts for Android”.

(Insert Star Wars joke here, p-unny this time.)

I’ve named mine “FirebaseQuickStart”. At this point, you may claim I’m not a creative person. Let’s just say you’re not totally wrong.

Let’s have a look at the project structure.

We’ll be using the messaging module. The sample code has almost everything however almost is the keyword here. Simply try to run/debug the app and you’ll get the following output:

> File google-services.json is missing. The Google Services Plugin cannot function without it.

“google-services.json” is the key file that connects your app to Firebase therefore sample project is not shipping with it. Either you have to create a new Firebase project or use the existing one to obtain this file. I’ll be creating a new one.

2. Creating the Firebase project

Visit https://console.firebase.google.com/ and name your new Project. I’ve named mine “FirebaseQuickStart” (yeah, I know that you know). On the next screen choose the Android icon to add a new Android app to your Firebase.

You can find the Android package name in your project’s “/app/build.gradle” file as applicationId. For the quickstart project it’s as follows:

com.google.firebase.quickstart.fcm

After registration download “google-services.json” and place it under the app folder.

Now you can simply run/debug your application.

If you were able to launch the application let’s try to send our first notification.

3. Testing the notification

When you visit the Firebase application on your browser you’ll be able to see Cloud Messaging on the left side under the Engage section.

You can compose your notification through this page. If you want to send a notification to all the devices you can simply publish it. However, if you want to target a specific device you should obtain its token in the first place. After launching your application, it’ll register itself to your Firebase project and Firebase will assign your device/emulator a token. If you visit your logcat you can obtain the token.

2020-12-20 01:49:55.727 10467-10525/com.google.firebase.quickstart.fcm D/MyFirebaseMsgService: Refreshed token: cg-DoPVYSr6_vDf3mZS4V8:APA91bEELrdJ-nqc2DgfyMWwLXKBo-DlWmWM0hx3kA3GknJOwCZpyi7xcaizZsmK1QsV8Ya9X2J1nynLTJt2KeixQCjeSyE7mWEOt9ax7kCHn9sq7e4Zw_PIpCo578buN07Acbe3w-ta

If you paste that value to “Send test message” input Firebase will send the notification to your device only.

When you test/publish the notification, this application won’t be showing anything if it’s in the foreground. Simply take the application to the background and send another.

That’s all folks.

Sincerely,
Burak.

--

--