FCM Notifications with API 26+ with reverse compatibility

Lattice Studios
Lattice Product Development
3 min readJul 15, 2018

With the release of API 26 (Android Oreo), FirebaseInstanceIdService was deprecated and notifications now require notification channels to work. I’ll be showing you how to set up FCM(Firebase Cloud Messaging) notifications for Android with for API 26+ and with reverse compatibility. All code will be provided in Kotlin. This is my first Medium article, so bear with me :)

In order to capture notifications from FCM, we need to set up a service for our app, namely an implementation of FirebaseMessagingService.

FCM targets devices by using encrypted, alpha numeric tokens that are unique to every device. They’re generated the first time you open up your app;

  • if the app deletes the token,
  • if the user installs then uninstalls, or
  • if the user clears their data.

FirebaseMessagingService will turn on automatically whenever a new token is generated for your device or when it picks up a notification from FCM. In order to pick up a FCM token, we have to override onNewToken in the FirebaseMessaginService.

Keep track of your FCM token that will be logged in your logcat, You’ll be needing it later to send notifications. If you ever lose it and need to retrieve it, you can use the following function to get your FCM token.

We also need to register our service in the manifest and ask for internet permissions.

API 26 now requires the use of notification channels, these channels can have different visual and auditory behaviors that users can modify in their notification settings.

All notification channels need to be named, so go ahead and name your channel.

When a notification is sent to your device, the onMesssageReceived is triggered in the FirebaseMessagingService. In order to handle the notification and display it to the user, we need to override onMessageRecieved. We’ll be getting to buildNotificationChannel() and sendNotification() functions in a moment.

Notification channels are only relevant for devices above API 26 so we need to have a statement that checks if the device in question is API 26+.

The following code is an example of how you can build a notification channel.

There are a number of different behaviors that you can manipulate for notification channels. A full list can be found at https://developer.android.com/reference/android/app/NotificationChannel

In order to display a notification for the user we need to use NotificationCompat’s Builder class. Before API 26, once could use NotificationCompat.Builder(Context), but that has been deprecated in favor of NotificationCompat.Builder(Context, NotificationChannel) to accommodate notification channels.

For API 26+, we can build a notification in the following way:

For devices lower than API 26:

Putting it all together, we get:

We check the API of the device to choose the appropriate way of building our notification. Notifications come with a number of customizable options, a full list can be found at https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder. Finally, we use notificationManager.notify() to display our notification to the user.

Your app is now ready to pick up notifications!

Let’s go ahead and test it out by heading over to the Firebase Console. Scroll down on the menu, select “Firebase Messaging” and create a new message. Grab your FCM token from your logs earlier, select “Single Device” in the console and paste your FCM token. Enter the text you want to display and press send!

Voila, you should see your device or emulator receiving the notification!

I hope this article was able to get you started with FCM notifications, thanks for your time. For more information on FCM for Android, visit https://firebase.google.com/docs/cloud-messaging/android/client.

--

--

Lattice Studios
Lattice Product Development

Digital Product Development + Creative Studio based out of Calgary, Canada. www.latticestudios.com