Technology is transforming people’s needs and expectations for communication. Many users want or need to receive reliable, timely alerts and notifications, even when they aren’t actively using an application. On mobile devices, push notifications serve this purpose. They look like any other alert on the device to inform users of messages or events, but they only reach users who’ve installed the application.
Push notifications are a powerful way for application publishers to engage users directly. Publishers can use them on most mobile platforms and at any time—you don’t need to be using a given application to receive them. They bypass spam filters and are frequently used to drive actions, like telling the user about a special discount, a weather event, or a financial transaction that needs to be confirmed.
In this article, you’ll learn how to implement push notifications using Expo, a platform for making universal native applications. You’ll see the steps and flow that push notifications go through to arrive on mobile devices and learn how you can send your own. While many of these steps can be done through the Expo platform, you’ll also take a look at using Courier to simplify the process.
Expo Push Notifications
Leveraging Expo, you can implement two types of application notifications:
Local notifications are triggered by the application installed on a device. This type of notification never involves sending notifications to other devices, such as when you receive a reminder from your calendar app.
Push notifications are sent to applications running on other devices. The receiving application needs to handle the notification and display it to the user, like when you receive a message on a chat app.
When targeting mobile devices, applications usually have to interact with notification gateways to send push notifications. These are services that offer APIs, which allow applications to send push notifications to users through HTTP requests. Targeting users on other devices, applications need to either integrate with Firebase Cloud Messaging (FCM) or the Apple Push Notification service (APNs), two of the most-used messaging platforms for mobile devices.
This means delivering notifications successfully requires you to configure FCM and APNs credentials and also requires appropriate handling of their APIs.
Push Notification Flow
To set up push notifications, the first step is to collect unique keys issued by FCM and APNs, called push tokens. They’re used to identify apps installed on devices and are necessary to send messages to specific devices. These keys need to be stored in a secure environment, such as backend infrastructure or a serverless solution, before calling the notification gateways.
You can choose to deal with the gateways directly, but this introduces many complexities—you would need to differentiate between native tokens (like Android and iOS), write code to call both gateways, and account for disparate error-handling procedures.
Instead, you can utilize Expo, which offers a push notification service to handle this behind the scenes, on behalf of the application. The Expo gateway manages the native device information and communicates with notification gateways. The expo-notifications package provides an API that facilitates fetching push notification tokens as well as presenting, scheduling, receiving, and responding to notifications.
For a more seamless solution, you can integrate Courier with Expo. Courier provides an intelligent notification infrastructure. Applications can emit events that are sent to Courier using their SDK or API. Courier receives the notification request, processes it, and then generates a notification template and sends it to the appropriate provider.
Implementing Expo Push Notifications
In this section, you’ll learn how to enhance your Expo application with push notifications. As you’ll see, using the Expo gateway directly and leveraging Courier to execute your push notifications is easy.
Send Notifications via the Expo Gateway
If you are just starting out developing Expo applications, you’ll first need to download some dependencies:
The Expo Go app enables you to open up your applications, served through the Expo CLI, and run your projects while developing them. You can find installation instructions for both of these dependencies here.
To use the Expo gateway, you’ll need to register with Expo. Run the command below to open a tab in your browser where you’re able to sign up:
Next, you need to log in with Expo, so that the application is able to send notifications through the gateway. Run the command below and supply your username and password:
You’re now ready to create your application. When you run the command below, you can create the skeleton of an Expo application. You’ll use this to build your application.
Before you can develop your application, or add code for sending and receiving notifications, you need to install the following packages:
Push notifications require the user's permission and for you to receive the user's ExpoPushToken, which is done through the expo-notifications library. To retrieve the permissions, use the requestPermissionsAsync and getExpoPushTokenAsync functions to get the ExpoPushToken. You can refer to the example below to see how this can be done:
Once you have the token, you are ready to send push notifications. The following code snippet is an example of a message template, which will be used to send a push notification.
sendPushNotification function creates a test message that will be sent as a push notification. Sending a POST request to the Expo gateway API, with a token identifying the target user, will send the notification. In this example, the target user is the application itself. In practice, this means the application will send a push notification to itself. Naturally, this means that you need to add the capability of receiving notifications, which you can see in the code below:
This snippet adds event listeners (
addNotificationResponseReceivedListener) that receive an object when a notification is received or interacted with. This snippet also adds a basic UI that allows you to test the application by clicking a button that sends the previously defined test message.
Once these steps are completed, you can use your application. Run the command below and scan the QR code with the Expo Go app to deploy the application to your device. An added benefit of using the Expo Go app is that any code changes will be automatically deployed to your device and ready to be tested immediately.
You can also use the Expo push notification tool, a web interface that allows you to send notifications. The code snipped above prints out the
ExponentPushToken, which you can use to test different messages.
Send Notifications Using Courier
Alternatively, you can register with Courier and leverage it to send your notifications. Once you have signed up, you need to configure a channel. Courier allows you to choose between FCM, APN, or Expo. For this example, you’ll configure an Expo channel, letting the Expo gateway deal with the notification gateways:
After successfully configuring the Expo channel, you need to retrieve the API keys to complete the Courier configuration:
Once configuration is done, change the application to integrate with Courier. First, install the @trycourier/courier package, which you can do by running the following command in your command line:
Next, change the
sendPushNotification function to send notifications through Courier instead of the Expo gateway, as shown in the example below:
Your application is now ready to send notifications through Courier, while the code to handle receiving notifications remains the same. The Expo Go app will automatically redeploy the application to your device so you can test it immediately.
The full working example application can be found here.
Push notifications are a powerful tool for application developers. You can use them for alerts, updates, critical communication, and as a valuable marketing tool for direct access to your users.
This direct access requires push notifications to go through notification gateways before arriving at the user’s devices. However, handling multiple gateways can be complex. Expo makes it easier to send push notifications through its notification gateway, and you can refine this process by integrating Courier.
Courier simplifies sending push notifications and offers seamless integration with Expo. It also provides a notification infrastructure capable of delivering notifications to a multitude of channels, like email or SMS. If you would like to learn more, check out our website and documentation.
Author: Ricardo Castro