Mobile Development

FCM Notifications With API 26+ With Reverse Compatibility

With the release of API 26 (Android Oreo), FirebaseInstanceIdService was deprecated and notifications now require notification channels to work...

Ali Elias - Lead Backend Developer

Sep 16, 2019

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.

https://gist.github.com/tbatsuur/7b8d343299ba2c81935a4e4e2cc8fd79#file-myfirebasemessagingservice-kt

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.

https://gist.github.com/tbatsuur/13ed1ed8afc6e35cd473d2a586ca5d2c#file-myfirebasemessagingservice-kt

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. https://gist.github.com/tbatsuur/4ce3cd903f7d4a03ad7c761b02056f59#file-grabfcmtoken-kt We also need to register our service in the manifest and ask for internet permissions.

https://gist.github.com/tbatsuur/da1559bbdb8a75de3bf7b24ba8deb6e0#file-permissions-xml 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.

https://gist.github.com/tbatsuur/ad818e6408fde02a8b01007e2f563573#file-default_notification_channel-kt 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. https://gist.github.com/tbatsuur/32d1ed652693a920149315dca581b944#file-onmessagereceived-kt 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+.

https://gist.github.com/tbatsuur/b9288de940d29b397d768c942d5fb871#file-buildnotificationchannel-kt The following code is an example of how you can build a notification channel.

https://gist.github.com/tbatsuur/6380b5441e3d5028a1d6d7da8a4cf3df#file-notificationchannel-kt 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(ContextNotificationChannel) to accommodate notification channels.

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

https://gist.github.com/tbatsuur/f7b10caae70dad1aa6bcaaa592399e06#file-notificationbuilder-kt For devices lower than API 26:

https://gist.github.com/tbatsuur/4d741b7dcc3415f649cd1ee8aea7fb0e#file-notificationbuilder26-kt Putting it all together, we get:

https://gist.github.com/tbatsuur/76c1db2e9eec8ec93c03c253ff202468#file-sendnotification-kt 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.

Continue Reading

#Business

Automate Your Business 7 Ways in 7 Days
Whether you are a head of a startup or a SME (Small/Medium Enterprise), automation is your friend. Think of automation like this...
Sep 16, 2019

#Business

Modern Brands That Use E-Commerce Really Well (Part I)
E-Commerce is an extremely powerful tool for any retail or brick and mortar business to invest in...
Sep 16, 2019

#Design

The Best Way to Define and Prioritize Features for Your MVP
Starting a business can be risky. Maybe you have a great business idea for a mobile app. Maybe you’ve done your market research to validate your concept...
Sep 16, 2019