Expo 推送通知设置

了解如何设置推送通知、获取开发和生产凭据以及发送测试推送通知。


要使用 Expo 推送通知服务,你必须通过安装一组库来配置你的应用,实现处理通知的函数,并为 Android 和 iOS 设置凭据。

¥To utilize Expo push notification service, you must configure your app by installing a set of libraries, implement functions to handle notifications, and set up credentials for Android and iOS.

完成本指南中概述的步骤或按照下面更详细的视频进行操作。最后,你将能够发送推送通知并在设备上接收它。

¥Complete the steps outlined in this guide or follow the more detailed video below. At the end, you'll be able to send a push notification and receive it on a device.

Expo Notifications with EAS | Complete Guide
Expo Notifications with EAS | Complete Guide

Learn how to set up push notifications in an Expo project. This video covers configuring Firebase for FCM v1 on Android, setting up Android and iOS credentials on EAS, building with EAS Build, and testing with Expo Notifications tool.


要让客户端准备好推送通知,需要执行以下操作:

¥To get the client-side ready for push notifications, the following things are required:

  • 用户向其发送推送通知的权限。

    ¥The user's permission to send them push notifications.

  • 应用的 ExpoPushToken

    ¥The app's ExpoPushToken.


Do you want to use FCM / APNs directly, instead of the Expo push notification service?

If you need finer-grained control over your notifications, communicating directly with FCM and APNs may be necessary. Expo does not lock you into using Expo Application Services, and the expo-notifications API is push-service agnostic. Learn how to "Send notifications with FCM and APNs".

Prerequisites

Important: Push notifications are not supported on Android Emulators and iOS Simulators. A real device is required.

The following steps described in this guide use EAS Build. This is the easiest way to set up notifications since your EAS project will also contain the notification credentials. However, you can use the expo-notifications library without EAS Build by building your project locally.

1

Install libraries

Run the following command to install the expo-notifications, expo-device and expo-constants libraries:

!!!IG2!!!

  • expo-notifications library is used to request a user's permission and to obtain the ExpoPushToken for sending push notifications.
  • expo-device is used to check whether the app is running on a physical device.
  • expo-constants is used to get the projectId value from the app config.

2

Add a minimal working example

The code below shows a working example of how to register for, send, and receive push notifications in a React Native app. Copy and paste it into your project:

!!!IG0!!!

Configure projectId

Using the previous example, when you are registering for push notifications, you need to use projectId. This property is used to attribute Expo push token to the specific project. For projects using EAS, the projectId property represents the Universally Unique Identifier (UUID) of that project.

projectId is set automatically when you create a development build. However, we recommend setting it manually in your project's code. To do so, you can use expo-constants to get the projectId value from the app config.

!!!IG1!!!

One advantage of attributing the Expo push token to your project's ID is that it doesn't change when a project is transferred between different accounts or the existing account gets renamed.

3

Get credentials for development builds

For Android and iOS, there are different requirements to set up your credentials.

!!!IG5!!!

!!!IG6!!!

For Android, you need to configure Firebase Cloud Messaging (FCM) to get credentials and set up your Expo project.

Follow the steps in Add Android FCM V1 credentials to set up your credentials.

!!!IG7!!!

!!!IG8!!!

A paid Apple Developer Account is required to generate credentials.

For iOS, make sure you have registered your iOS device on which you want to test before running the eas build command for the first time.

If you create a development build for the first time, you'll be asked to enable push notifications. Answer yes to the following questions when prompted by the EAS CLI:

  • Setup Push Notifications for your project

  • Generating a new Apple Push Notifications service key

!!!IG9!!!

!!!IG10!!!


If you are not using EAS Build, run eas credentials manually.

4

Build the app

!!!IG3!!!

5

Test using the push notifications tool

After creating and installing the development build, you can use Expo push notifications tool to quickly send a test notification to your device.

  1. Start the development server for your project:

!!!IG4!!!

  1. Open the development build on your device.

  2. After the ExpoPushToken is generated, enter the value in the Expo push notifications tool with other details (for example, a message title and body).

  3. Click on the Send a Notification button.

After sending the notification from the tool, you should see the notification on your device. Below is an example of an Android device receiving a push notification.