Platform
Docs
Solutions
ContactLog In

Watch, develop, learn!

Build exceptional notification experiences in web and mobile apps.

See all events

PRODUCT NEWSENGINEERING

Building Android push notifications with Firebase and Courier’s SDK

Mike Miller

March 21, 2023

Push notifications have become an essential part of modern mobile apps, allowing you to keep your users engaged and informed. However, implementing push for different platforms can be a complex and time-consuming task, requiring developers to set up and handle token management, testing, and other logistical details.

android-push-notifications-with-courier

Courier's Android Mobile Notifications Software Development Kit (SDK) is designed to make it easy for you to implement push notifications in your mobile apps. The SDK provides a set of tools that simplify the process of managing user state, handling tokens, and sending test notifications. Some of these terms may be unfamiliar, but we’ll explain them in this article and show you how to use the SDK. Before diving into how to use the Courier Android SDK, first let's touch on the benefits it provides. Later, we will go into more depth.

  • Manage tokens easily: The SDK simplifies the process of handling tokens, which can be a complex and time-consuming task for developers to set up and manage. When implementing push notifications, developers need to handle token management, which involves generating and storing unique identifiers for each device that can receive notifications.
  • Manage user state effectively: The SDK also simplifies the process of managing user state, which refers to the current status or context of a user within an app.
  • Send test notifications: The SDK includes tools that enable developers to send test notifications, ensuring that their notifications are working as intended before they go live. This can save time and prevent potential issues.

By providing these tools, the Courier Android SDK helps developers streamline the process of implementing push notifications in their apps. With these benefits in mind, let's move on to how to get started with the SDK.

Getting started with Courier

To get started with the Courier Android SDK, you will need to have the following:

  1. Android Studio or any other development environment that supports Android development. The SDK supports Java, Kotlin, and Firebase Cloud Messaging.
  2. A valid Firebase project. Although the Courier SDK itself doesn’t need a Firebase account, if you want to send push notifications to your app you will need a Firebase account.
  3. A physical Android device for testing. Testing push notifications with or without the Courier SDK does not work well with emulators.
  4. A Courier account. You need an account in order to use the SDK.
  5. The SDK. Once you have the other prerequisites, you can download the SDK and include it in your project.

To install the Courier Android SDK have a look at the step-by-step guide on our GitHub page.

Once you’ve successfully set up the Courier SDK in your Android app, let's dive into some of the features of the SDK.

A note to the developers amongst you: Some of the functions that we will cover below depend on an AppCompatActivity, just something to keep in mind as we move forward.

start-sending-push-notifications-with-courier

Managing user state

Before we think about sending notifications, it's important to consider the user state. This is the status of a user in relation to your app, e.g., signed in or out. This can impact the user's experience with your app. For instance, if your user is signed out, they shouldn’t be receiving notifications as if they are still signed in. For an optimal user experience, it's best to keep the user's push notification tokens in sync with their state. Most of this is handled automatically by Courier. In order to manage the user state in your app, you can use the functions provided by Courier. By calling the Courier.shared.signIn() function, you can ensure that the user's push notification tokens and their state are kept in sync.

1
Courier.shared.signIn(
2
accessToken = authKey_or_accessToken,
3
userId = userId
4
)

The accessToken can either be a Courier Auth Key used during development or a JWT (JSON Web Token) that should be used in your production app.

When the user signs out, you can use the Courier.shared.signOut() function to clear their state.

1
Courier.shared.signOut()

By following these steps, the FCM (Firebase Cloud Messaging) tokens on Android will be automatically synced to Courier, making it easier for you to manage the user's state and send relevant and timely push notifications.

Push notification permissions

To make a great user experience, you’ll need to request push notification permissions at the right time in your user’s flow. Finding the code to request these permissions can be difficult. . Courier makes it easy to check permissions with the requestNotificationPermission() function.

1
if (requestNotificationPermission()) {
2
//Send some notification
3
}

The requestNotificationPermission() function returns a Boolean that tells us if the user has permissions on or off. Then, you can continue with your code, perhaps sending a test notification to the device to make sure everything is working well for the user experience you are building. In newer versions of Android (version 33 and up) this will present a popup asking your users for push notifications permissions. In older versions of android, this will always return true.

Simple delivery tracking

Courier has an onPushNotificationDelivered() function, which is triggered when a push notification is delivered to the device. This is useful for keeping track of delivery status.

1
override fun onPushNotificationDelivered(message: RemoteMessage) {
2
print(message)
3
}

Bear in mind that this function will only be triggered when the app is in the foreground or potentially background state. It will not be called when the app is in the "killed" or "not running" state. It also must be inside of a CourierActivity (which is a subclass of AppCompatActivity).

Message received?

You know how to check if the notification was delivered, but what if you want to know when the user interacts with it? That’s where the onPushNotificationClicked() function comes in. This function is triggered when the user taps on a push notification. You can use this function to handle what happens when the user interacts with the notification and must also be inside of a CourierActivity (which is a subclass of AppCompatActivity)

1
override fun onPushNotificationClicked(message: RemoteMessage) {
2
print(message)
3
}

In the example above, we are simply printing a message, but you can replace it with your own logic such as opening a specific activity or displaying some interface component.

Sending a test push

With permissions handled, and some understanding of how we can track the status of our notifications, lets see how sending notifications works. Courier enables you to send push notifications directly to a user's ID via the SDK, without the need to make a direct call to Firebase from your backend or through a raw HTTP call. This ID is the userId you register data with in Courier. This should probably match the user IDs you use in your existing authentication system. For now, let’s look at how you can use the sendPush() function to send test push notifications. Here's how to send a test push.

1
val messageId = Courier.shared.sendPush(
2
authKey: 'a_courier_auth_key_only_for_testing',
3
userId: 'example_user',
4
title: 'Hello!',
5
body: 'This is a push message from Courier 🐣',
6
providers: [CourierProvider.fcm],
7
)

This code will send a push notification to every valid token for the given userId and providers (e.g., FCM) that you declare. So be careful and make sure to only use it for testing purposes. It's important to note that the authKey you use in this example is for testing purposes only and should not be used in production.

Let's talk about tokens

One of the most challenging aspects of push notifications is managing tokens: unique identifiers that are used to identify a specific device for push notifications. They are typically generated and managed by the operating system's push notification service, such as Firebase Cloud Messaging (FCM) for Android and Apple Push Notification Service (APNS) for iOS. To send push notifications to a specific device or user, you must use the device's token. Your app needs to store tokens securely on the back end and associate them with the relevant user or device. Additionally, tokens should be regularly updated, as they can change or expire. Token management also includes handling token deletion when a device or user unsubscribes from push notifications or uninstalls the app. Setting up your own token management infrastructure can require iteration between front-end (or mobile) and back-end developers. This back-and-forth can take several days or even a month, between designers, mobile engineers building the functionality into the app, and then the back-end engineer building the infrastructure to make it work. Courier takes care of all of the above for you! You don’t need to learn all the logistics of token management, because Courier's mobile SDKs automatically takes push notification tokens, stores them in Courier, and automatically invalidates tokens that expire, allowing you to focus on building your product.

Conclusion

In this post you have seen how Courier streamlines the hassle of handling push notifications. It takes care of token generation, storage, and removal, allowing you to concentrate on building your app without worrying about the technicalities of token management. You’ve seen how the SDK is user-friendly, with a straightforward setup and integration process that can be completed with just a few lines of code, even in existing projects. Courier has other SDKs available. Whether you're working with Android, iOS, React Native or Flutter, Courier has an SDK that can help streamline your push notification process. So, if you're looking for a way to simplify your push notification management, give Courier a try.

Watch, develop, learn!

Build exceptional notification experiences in web and mobile apps.

See all events

More from Product News

Delivery Window Thumb
PRODUCT NEWS

Smart Notification Scheduling with Delivery Windows

Timing is everything. Courier’s new Delivery Window feature ensures your notifications arrive at the perfect moment—no more off-hours pings or complex scheduling code. Define the rules, and Courier handles the rest, adapting seamlessly to time zones and user preferences.

Seth Carney

Seth Carney

December 16, 2024

Courier Inbox showing an example of the notification center UI with the bell icon lighting up with new unread messages
PRODUCT NEWS

Courier Inbox for web and mobile, a complete notification center

A notification center inside of web and mobile apps is now an expectation. It’s a way to reach specific audiences or users with tailored messages and a way to boost engagement by bringing people back into the app. While Courier has been adding Inbox capabilities over the last couple years, we’re excited to announce a complete set of SDKs that span web and mobile. You can drop in a full-featured inbox to give your users a best-in-class notification center inside your app that works seamlessly with your existing notification flows.

Donnie Wang

Donnie Wang

June 14, 2023

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Build your first notification in minutes

Send up to 10,000 notifications every month, for free.

Get started for free

Email & push notification

Platform

Users

Content

Channels

Sending

Workflows

Preferences

Inbox

Workspaces

Observability

API Status

Changelog

© 2024 Courier. All rights reserved.