React Native iOS端脱离Expo Go发送远程推送通知方案求助
基于Expo实现脱离Expo Go的个性化远程推送方案
核心思路
要脱离Expo Go接收推送,必须使用EAS Build构建独立原生包(不能依赖Expo Go沙盒环境),直接对接Android FCM、iOS APNs原生推送服务,配合官方或第三方推送库实现个性化推送,同时通过自有Lambda触发单用户定向推送。
方案一:使用Expo Notifications(官方推荐,适配Expo生态)
1. 项目配置与依赖安装
- 安装核心依赖:
npx expo install expo-notifications expo-device
- 在
app.json中配置推送权限与原生服务:
{ "expo": { "ios": { "infoPlist": { "UIBackgroundModes": ["remote-notification"], "NSUserNotificationUsageDescription": "接收健身提醒推送" } }, "android": { "permissions": ["POST_NOTIFICATIONS"], "googleServicesFile": "./google-services.json" // 上传FCM官方配置文件 } } }
2. 客户端注册推送令牌
替换原native-notify的注册逻辑,改为获取原生推送令牌并与用户ID绑定存储:
import * as Notifications from 'expo-notifications'; import * as Device from 'expo-device'; async function registerPushToken(userID) { let token; if (Device.isDevice) { // 检查并请求推送权限 const { status: existingStatus } = await Notifications.getPermissionsAsync(); let finalStatus = existingStatus; if (existingStatus !== 'granted') { const { status } = await Notifications.requestPermissionsAsync(); finalStatus = status; } if (finalStatus !== 'granted') { alert('无法获取推送权限'); return; } // 获取Expo推送令牌 token = (await Notifications.getExpoPushTokenAsync()).data; // 将令牌与用户ID关联,存储到你的后端数据库 await fetch('你的后端存储接口地址', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userID, pushToken: token }) }); } else { alert('仅真机环境支持推送功能'); } return token; } // 应用启动时调用 registerPushToken(user.attributes.email);
3. Lambda端触发个性化推送
通过Expo官方推送API发送单用户定向推送,替换原native-notify的请求逻辑:
await axios.post('https://exp.host/--/api/v2/push/send', { to: 目标用户的Expo推送令牌, // 从你的数据库根据userID查询获取 sound: 'default', title: 'Gym Time!', body: `Today is your ${item[today]} day! Go get em!`, data: { /* 可选:附加自定义业务数据 */ } }).then(res => { console.log('推送发送成功', res); }).catch(err => { console.log('推送发送失败:', err); });
方案二:使用notifee(原生推送自定义能力更强)
如果需要自定义通知样式、分组、触发条件等复杂逻辑,可选用notifee配合原生推送服务:
1. 依赖安装与配置
- 安装依赖:
npx expo install @notifee/react-native
- 按照官方文档配置FCM/APNs证书与服务信息(需在EAS Build中完成原生配置)
2. 客户端注册与令牌获取
import notifee from '@notifee/react-native'; async function registerPushToken(userID) { // 请求推送权限 await notifee.requestPermission(); // 获取原生推送令牌(FCM/APNs原生令牌) const token = await notifee.getToken(); // 存储令牌到后端关联用户ID await fetch('你的后端存储接口地址', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userID, pushToken: token }) }); } // 应用启动时调用 registerPushToken(user.attributes.email);
3. Lambda端触发推送
直接调用FCM/APNs官方API发送定向推送:
- Android(FCM)示例:
await axios.post('https://fcm.googleapis.com/fcm/send', { to: 目标用户的FCM令牌, notification: { title: 'Gym Time!', body: `Today is your ${item[today]} day! Go get em!`, sound: 'default' } }, { headers: { 'Authorization': 'key=你的FCM服务器密钥', 'Content-Type': 'application/json' } });
- iOS(APNs):需通过APNs HTTP/2 API发送,可使用
apn等工具包简化请求逻辑。
关键注意事项
- 必须使用EAS Build构建独立原生包,Expo Go沙盒环境无法支持自定义原生推送通道
- 推送令牌必须与用户ID绑定存储,Lambda发送时通过用户ID查询对应令牌实现个性化推送
- 确保FCM/APNs的配置文件、证书已正确配置到EAS Build项目中
内容的提问来源于stack exchange,提问作者branman3542
相关产品推荐
相关产品推荐

