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.
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. 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(Context, NotificationChannel) 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.