使用推送通知

了解与 Expo 和 React Native 应用兼容的推送通知服务。


Expo 应用可以与任何通知服务或 Android 和 iOS 操作系统提供的任何通知功能配合使用。即使某个功能的包尚不存在,也可以编写原生代码通过 Expo 模块 API 访问它,并且可以使用 配置插件 自动化原生项目配置。以下选项提供专门构建的 Expo 集成,包括必要的配置插件,用于在你的应用中实现推送通知:

¥Expo apps can work with any notification service or any of the notification capabilities offered by the Android and iOS operating systems. Even if a package doesn't yet exist for a feature, native code can be written to access it via the Expo Modules API, and native project configurations can be automated using config plugins. The following options provide purpose-built Expo integrations, including config plugins where necessary, for implementing push notifications in your app:

expo-notifications 库经过设计和测试,可与 Expo 的推送通知服务以及直接从 FCM 和 APNS 发送的通知配合使用。某些高级功能可能与第三方提供商不兼容,因为他们通常有自己的原生和针对其服务优化的 React Native SDK。

¥The expo-notifications library is designed and tested to work with Expo's push notification service and notifications sent directly from FCM and APNS. Some advanced features may not be compatible with third-party providers, as they often have their own native and React Native SDKs optimized for their services.

Expo 推送通知

¥Expo push notifications

Expo 通知 提供了一个统一的 API,用于处理 Android 和 iOS 上的推送通知。它与你的 Expo 账户无缝集成,并且可以免费使用。

¥Expo Notifications provides a unified API for handling push notifications across Android and iOS. It integrates seamlessly with your Expo account and is free to use.

关键功能

¥Key capabilities

注意事项和限制

¥Considerations and limitations

  • 用于向通知添加其他内容(例如图片)的 iOS 通知扩展尚未正式包含或支持,但你可以使用自定义原生代码和配置添加它们。

    ¥iOS notification extensions for adding additional content to notifications, such as images, are not formally included or supported, but you can add them with custom native code and configuration.

  • 每个项目每秒的通知量限制为 600 条。

    ¥Volumes are limited to 600 notifications per second per project.

有关实现详细信息,请参阅以下指南:

¥For implementation details, see the following guides:

Expo 推送通知概述

了解有关 Expo 推送通知的更多信息。

Expo Notifications 服务器端 SDK 选项

了解有关使用服务器发送推送通知的更多信息。

OneSignal

OneSignal 是一个客户互动平台,可为网络和移动应用提供推送通知、应用内消息、短信和电子邮件服务。OneSignal 支持通知和参与度分析中的富媒体。它包括一个 Expo 配置插件,可直接集成到你的 Expo 项目中。

¥OneSignal is a customer engagement platform that provides push notifications, in-app messaging, SMS, and email services for web and mobile apps. OneSignal supports rich media in notifications and engagement analytics. It includes an Expo config plugin for direct integration into your Expo project.

OneSignal Expo SDK 设置

按照本指南,逐步了解如何将 OneSignal 集成到你的 Expo 项目中。

Braze

Braze 是一个客户互动平台,可通过推送通知、应用内消息、电子邮件、短信和网络提供个性化的跨渠道消息传递。Braze 支持丰富的通知内容、推送通知活动,并支持在 Android 上交付失败后重新发送通知。它提供了 React Native SDK配置插件。查看 Expo 示例应用 了解更多详细信息。

¥Braze is a customer engagement platform that delivers personalized, cross-channel messaging through push notifications, in-app messaging, email, SMS, and web. Braze supports rich notification content, push notification campaigns, and support for resending notifications after failed deliveries on Android. It provides a React Native SDK and a config plugin. Check out the Expo example app for more details.

Braze Expo 设置

按照本指南,逐步了解如何将 Braze 集成到你的 Expo 项目中。

Customer.io

Customer.io 是一个客户参与平台,可让你利用推送通知、应用内消息、电子邮件、短信功能等设计强大的自动化工作流程。其可视化工作流构建器允许你跨多个渠道自动化复杂的数据驱动活动。Customer.io 支持设备端指标收集,可用于根据用户行为和偏好定制推送通知。Customer.io 提供 Expo 插件 以直接与你的 Expo 项目集成,并提供使用 Customer.io 推送通知以及其他提供商的文档。

¥Customer.io is a customer engagement platform that allows you to design powerful automated workflows utilizing push notifications, in-app messaging, email, SMS capabilities, and more. Its visual workflow builder allows you to automate complex, data-driven campaigns across multiple channels. Customer.io supports device-side metrics collection that can be used to customize push notifications tailored to user behaviors and preferences. Customer.io provides an Expo plugin for direct integration with your Expo project and documentation for using Customer.io push notifications alongside other providers.

Customer.io Expo 快速入门指南

按照本指南,逐步了解如何将 Customer.io 集成到你的 Expo 项目中。

通过 FCM 和 APN 直接发送通知

¥Send notifications directly via FCM and APNs

你可以选择从后端直接发送到平台推送 API。在这种情况下,你仍然可以使用 expo-notifications 检索原生推送令牌并为每个平台单独配置通知。

¥You may choose to send directly to platform push API's from your backend. In this case, you can still use expo-notifications to retrieve the native push token and configure notifications separately for each platform.

尽管客户端代码与 expo-notifications 保持跨平台,但你需要实现服务器端逻辑以分别与 FCMAPNs API 交互。

¥Although the client-side code remains cross-platform with expo-notifications, you will need to implement server-side logic to interact with the FCM and APNs APIs individually.

React Native Firebase 消息传递

¥React Native Firebase messaging

React Native Firebase 提供了一个消息模块,让你可以将 Firebase 云消息传递 (FCM) 用作 Android 和 iOS 的统一推送通知服务。虽然 FCM 通常与 Android 通知相关联,但它也通过在后台通过 Apple Push Notification 服务 (APN) 路由消息来支持 iOS。

¥React Native Firebase provides a messaging module that lets you use Firebase Cloud Messaging (FCM) as a unified push notification service for both Android and iOS. While FCM is often associated with Android notifications, it also supports iOS by routing messages through Apple Push Notification service (APNs) behind the scenes.

这种方法不同于仅将 FCM 用于 Android 通知。相反,Firebase 的跨平台 SDK 通过单一服务处理两个平台的通知。

¥This approach differs from using FCM solely for Android notifications. Instead, Firebase's cross-platform SDK handles notifications for both platforms through a single service.

即使 FCM 处理两个平台的通知,iOS 通知仍会通过 APN。Firebase 会自动管理此路由。在 React Native Firebase 消息传递文档 中了解更多信息。

¥Even though FCM handles notifications for both platforms, iOS notifications still go through APNs. Firebase automatically manages this routing. Learn more in the React Native Firebase messaging documentation.

提示和重要提示注意事项

¥Tips and important considerations

  • 避免混合客户端实现:不同的通知服务可能有冲突的客户端实现。使用一致的方法防止潜在问题。

    ¥Avoid mixing client-side implementations: Different notification services may have conflicting client-side implementations. Use a consistent approach to prevent potential issues.

  • Web 通知:Expo 通知不支持 Web 通知。但是,某些第三方解决方案可能会提供此功能。选择服务时,请考虑应用的要求。

    ¥Web notifications: Expo notifications do not support web notifications. However, some third-party solutions may offer this capability. Consider your app's requirements when choosing a service.

  • 令牌管理:在你的数据库中跟踪 Expo 推送令牌和原生设备令牌。这为未来的集成提供了灵活性,尤其是通过 FCM 或 APN 直接发送通知的营销工具。

    ¥Token management: Track both Expo push tokens and native device tokens in your database. This provides flexibility for future integrations, especially with marketing tools that send notifications directly via FCM or APNs.