用途:为 React Native / Expo 应用接入推送通知,覆盖权限、token 获取、通知接收与后端发送,以及 Firebase 云消息(FCM)与 Apple 推送(APNs)相关设置。
主要能力:使用 Expo Notifications 统一处理通知展示、声音、角标;申请通知权限并获取 Expo Push Token;监听前台收到的通知和用户点击通知后的响应,可据 data 做页面跳转;通过 Expo 推送发送接口从服务器下发通知;在 bare workflow 下使用 @react-native-firebase/messaging 获取 FCM token 并设置后台消息处理器。
使用方式:安装依赖 npx expo install expo-notifications expo-device。配置 Notifications.setNotificationHandler 控制前台展示行为;registerForPushNotifications 中检查 Device.isDevice,读取并请求权限,未授权返回 null,授权后调用 Notifications.getExpoPushTokenAsync({ projectId: 'your-project-id' }) 取得 token。用 addNotificationReceivedListener 监听前台通知,用 addNotificationResponseReceivedListener 处理点击并读取 data 导航。后端通过 POST https://exp.host/--/api/v2/push/send 发送,body 含 to、title、body、data。bare workflow 中用 messaging().getToken() 取 FCM token,messaging().setBackgroundMessageHandler 处理后台消息。
适用场景:Expo 项目或 React Native bare 项目的消息提醒、聊天通知、跳转类推送。
限制:示例中的 projectId、token 等需替换为真实值;权限与平台配置、APNs 证书、服务端密钥等细节资料未提供;真机以外无法获取有效推送 token(示例中非设备返回 null)。