Posted on

Android Push Notifications w/ FCM


android push notifications

 

Designed by Freepik

If you have an app and want to add push notifications to it, there’s nothing as simple as implementing them with Firebase Cloud Messaging. In just 10 minutes you’ll have your notifications up and running, let’s see how to do that

Setup App

If you have Firebase set up for your app – perfect. Otherwise you’ll need to create Firebase project here, add Android app and download google-services.json to your app directory. I’ll use this app from posts about using RecyclerView with Geofire and Firebase Cloud Functions. This repo has google-services.json in there because I didn’t want you to have all that pain creating Firebase projects just for testing it up. So you can download it and try stuff yourself immediately.

Aside from adding google-services.json file what we do as well is adding google services Gradle plugin in project’s build.gradle

And apply it as a last line in module’s build.gradle and adding Firebase Messaging dependency

Send First Message

There’s three ways to send push notifications:

  •  In Firebase Console
  • Using FCM server
  • Using your own server

Let’s see at the simplest one, Firebase Console. First you need to install app. If you’re using mine, then you won’t be able to send those notifications from console since you have no access to my Firebase project, go to the next step then.

Open your project in Firebase Console, navigate to Notifications menu, New Message, enter some text to title and select your app’s package for segment setting, then send message

android push notifications

You’ll see push notification automatically if your app was in background

Using FCM Server

Of course you want to send notifications programmatically, which leads us to the second way is by using FCM Server. Which means that you send queries to Firebase server with bunch of data like your authentication key, message title, body etc. You can get reference here, but I prefer to write code once and use abstraction on clients by using next approach.

Create Admin Server

This is my preferred one because you can write code once and use it on every platform: Android, iOS, web super easy. If you followed my previous post about Firebase Cloud Functions, than you probably have Node.js server running already, it’s a perfect way to go if you use Firebase for your apps already.

If not, it’s still a faster way than creating Node.js on other hosts or any other Java server, because Firebase Admin SDK is available in Java and JS.

Cloud Function Endpoint

Here’s JavaScript endpoint

And deploy it with

Once again, check this post about how to create this Cloud Functions server.

You can test your code just by running url in browser, it will be something like this: https://firebaseProjectName.cloudfunctions.net/sendNotification?to=all&title=hello&body=thisIsBody

to attribute accepts all to send to everybody or device token which we’ll get on Android client

Android Side

In admin SDK there’s no Segments like we used in Firebase Console, that’s why we’ll use Topics. Users subscribe to ones, all of our users will subscribe to topic all using this line of code in MainActivity

Now reinstall the app and you’ll be getting notifications directed to every user. To send notifications from Android just run query with that url from above using whatever networking library you prefer. I had Retrofit endpoints for my cloud functions, so I simply created one more.

Get Your Messaging Token

What about notification to single user? You need to get your messaging token by adding this service to manifest

And actual Java class

refreshedToken is what you want to use as to parameter to our endpoint. Why creating this kind of service and not just run in anywhere in code?  Because you might get a null if you call it first time your app installed and this token isn’t permanent, so you definitely wanna know when it’s being refreshed. You just save it to your database as a field of user model and can retrieve each time you want to send him push notifications

Custom Notifications

As you see it’s super easy to setup Push Notifications using FCM. And you got your notifications when app’s in background automatically. Once thing is that if you don’t use Firebase database for your apps and have server not in Java or JS then it will be probably easier to use FCM Server than creating another one.

For most cases that’s all you’ll need, but what if you want to create custom notifications or add quick reply from Android 7.0? Or it’s not a push notification at all, but rather some background message you need to handle and not display as push notification?

We can do that pretty easy! Add this service to manifest

onMessageReceived will be called when app in foreground only though and you still get push notifications automatically. Why is that? There’s two types of messages: Notifications and Data. If you go back to our JavaScript, you’ll see notification object in payload. Just by changing it to data we won’t have any auto push notifications displaying

Now onMessageReceived is called for each message and you can create your own push notifications or anything else with that data by extracting them from this map

 

Alright, hope this was helpful. You can get Android source code here, it has everything set up so you can just start sending push notifications to everybody who has this app installed. Backend JS code here. Don’t forget to subscribe, follow me on Facebook, Twitter, G+ and share with friends if you think this will benefit them!

 

 


  • Andres Garcia

    We can also use OneSignal, which is a Free and Reliable Push Notification Service.
    Much simpler and quicker than dealing with Node.js etc etc. Also very stable, even at very high volumes (500K+).
    Just saying, from a Dev to another Dev ^^