推送通知让应用即使在未打开时也能通知用户——用于消息、更新和用户参与。在 React Native 中,它们涉及平台服务(iOS 的 APNs、Android 的 FCM)、权限、设备令牌和处理通知事件。
推送通知的工作原理(流程)
1. The app requests notification PERMISSION from the user (especially iOS — opt-in)
2. The device registers and gets a unique push TOKEN (identifies this device/app install)
3. The app sends the token to YOUR SERVER (stores it to target this device)
4. Your server sends a notification → via the platform service:
APNs (Apple Push Notification service) for iOS, FCM (Firebase Cloud Messaging) for Android
5. The platform delivers it to the device → the app shows/handles it
→ Notifications work even when the app is closed (the OS handles delivery).
在 React Native 中实现
→ LIBRARIES handle the complexity:
- Expo Notifications (managed, simpler) — for Expo apps
- @react-native-firebase/messaging (FCM) — common for bare/cross-platform
- Notifee — rich local/displayed notifications
→ Request permission, get the token, send it to your backend, handle received notifications
