React Native Amplify集成PinPoint更新端点报错:byteLength未定义
React Native Amplify Pinpoint updateEndpoint 报错:TypeError: Cannot read property 'byteLength' of undefined
问题背景
我是Amplify新手,已通过CLI完成React Native项目配置,当前资源状态如下:
| Category | Resource name | Operation | Provider plugin |
|---|---|---|---|
| Auth | userPoolGroups | No Change | awscloudformation |
| Auth | Rent | No Change | awscloudformation |
| Analytics | RentAppIOS | No Change | awscloudformation |
| Notifications | RentAppIOS | No Change |
尝试配置iOS PinPoint推送通知,权限正常,但调用Analytics.updateEndpoint创建端点时触发以下错误:
AWSPinpointProvider - updateEndpoint failed [TypeError: Cannot read property 'byteLength' of undefined]
调用的报错函数:
const registerForPushNotifications = async () => { const { attributes: {sub} } = await Auth.currentUserInfo(); if (sub) { DeviceInfo.getDeviceToken().then((deviceToken) => { Analytics.updateEndpoint({ address: deviceToken, optOut: "NONE", userId: sub, channelType: "APNS", }).then(() => { console.log("Endpoint created"); }).catch((error) => { console.log('Error updating endpoint', error); }); }); } }
已确认sub和deviceToken值符合预期,应用初始化配置代码:
Amplify.configure({ ...awsconfig, Analytics: { disabled: false, }, Predictions: { provider: AmazonAIPredictionsProvider, region: awsconfig.aws_mobile_analytics_app_region, }, }); Analytics.configure({ ...awsconfig, AWSPinpoint: { region: awsconfig.aws_mobile_analytics_app_region, }, }); PushNotification.configure({ ...awsconfig, onNotification: function (notification) { console.log('NOTIFICATION:', notification); Alert.alert( notification.title, notification.body, [{ text: 'OK' }], { cancelable: false } ); notification.finish("backgroundFetchResultNewData"); }, permissions: { alert: true, badge: true, sound: true, }, popInitialNotification: true, requestPermissions: true, });
aws-exports.js内容:
const awsmobile = { aws_project_region: "eu-west-2", aws_cognito_identity_pool_id: "eu-west-2:xxxxxxxx-c514-4431-91c0-xxxxxxxx", aws_cognito_region: "eu-west-2", aws_user_pools_id: "eu-west-2_xxxxxxxx", aws_user_pools_web_client_id: "xxxxxxxxxxxxxxxxxxx2m5pmf", aws_pinpoint_id: "b6846xxxxxxxxxxxxxxxxxxxxxxxxxxx", aws_mobile_analytics_app_id: "xxxxxxxxxxete", aws_mobile_analytics_app_region: "eu-west-2", aws_mobile_analytics_app_title: "Rent", aws_mobile_analytics_auto_session_record: true, aws_mobile_analytics_disabled: false }; export default awsmobile;
解决方案
1. 补全Analytics配置中的Pinpoint App ID
byteLength未定义的核心原因通常是Pinpoint客户端初始化时缺少appId参数,导致请求签名或请求体生成失败。修改Analytics.configure代码:
Analytics.configure({ ...awsconfig, AWSPinpoint: { region: awsconfig.aws_mobile_analytics_app_region, appId: awsconfig.aws_pinpoint_id, // 新增此行绑定Pinpoint ID }, });
2. 统一Amplify相关包版本
版本不匹配可能导致内部参数传递异常,执行命令统一核心包版本:
npm install @aws-amplify/core@latest @aws-amplify/analytics@latest @aws-amplify/pushnotification@latest
3. 格式化iOS DeviceToken
iOS返回的DeviceToken可能包含<>或空格,需先清理:
DeviceInfo.getDeviceToken().then((deviceToken) => { // 去除特殊字符和空格 const formattedToken = deviceToken.replace(/[<> ]/g, ''); Analytics.updateEndpoint({ address: formattedToken, optOut: "NONE", userId: sub, channelType: "APNS", }) // 后续代码不变 });
4. 调整初始化顺序
确保Amplify基础配置优先执行,后续模块按依赖顺序初始化:
// 1. 基础Amplify配置 Amplify.configure({ ...awsconfig, Analytics: { disabled: false, }, Predictions: { provider: AmazonAIPredictionsProvider, region: awsconfig.aws_mobile_analytics_app_region, }, }); // 2. Analytics配置 Analytics.configure({ ...awsconfig, AWSPinpoint: { region: awsconfig.aws_mobile_analytics_app_region, appId: awsconfig.aws_pinpoint_id, }, }); // 3. PushNotification配置 PushNotification.configure({ ...awsconfig, // 原配置内容不变 });
5. 验证IAM权限
检查Cognito身份池对应的IAM角色是否包含Pinpoint操作权限,策略需包含:
{ "Effect": "Allow", "Action": [ "mobileanalytics:PutEvents", "pinpoint:UpdateEndpoint", "pinpoint:GetEndpoint", "pinpoint:PutEvents" ], "Resource": [ "arn:aws:pinpoint:eu-west-2:YOUR_ACCOUNT_ID:apps/YOUR_PINPOINT_APP_ID/*" ] }
验证步骤
修改配置后重新打包iOS应用,登录触发registerForPushNotifications函数,检查控制台日志是否显示「Endpoint created」,同时可在Pinpoint控制台的「端点」页面确认新端点生成。
内容的提问来源于stack exchange,提问作者Paul
相关产品推荐
相关产品推荐

