Expo Go中React Native推送通知不显示求助(返回状态正常)
Expo推送通知无法显示的排查与修复方案
代码中的直接问题
- 移除冗余代码:
registerForPushNotificationsAsync函数里的Notifications.getDevicePushTokenAsync;未实际调用,属于无效代码,直接删除即可。
核心排查步骤
1. 验证推送请求Payload格式
请求返回ok不代表Payload符合Expo要求,Android必须指定channelId(你代码中已创建名为default的频道)。正确的Payload示例:
{ "to": "你的Expo设备Token", "title": "测试标题", "body": "测试内容", "sound": "default", "channelId": "default" }
缺少channelId会导致Android 8.0+设备无法展示通知。
2. 确认设备Expo账号登录状态
Expo Go必须登录与你的项目关联的Expo账号,否则推送无法路由到设备。打开Expo Go侧边栏,检查账号是否匹配。
3. 检查应用状态与通知权限
- 前台状态:你的
setNotificationHandler已设置shouldShowAlert: true,若前台仍收不到,在notificationListener回调中添加console.log(notification),验证监听器是否触发。 - 后台/杀死状态:
- Android:确认系统设置中开启了应用的通知权限,且
default频道的优先级为MAX(可在系统通知设置中查看)。 - iOS:检查设备设置中允许该应用发送通知,包括锁屏、横幅等权限。
- Android:确认系统设置中开启了应用的通知权限,且
4. 验证Token有效性
- 确认推送使用的Token与控制台打印的
token完全一致,通过AsyncStorage.getItem("DeviceToken")验证存储的Token是否正确。 - 若Token过期或无效,重新注册并更新后端存储的Token值。
5. 网络与环境检查
- 设备需处于联网状态,推送依赖Expo服务器,确保网络可正常访问Expo服务。
- 确认项目的Expo SDK版本与
expo-notifications版本兼容,避免版本不匹配引发问题。
代码优化建议
完善registerForPushNotificationsAsync的错误处理,明确Token获取失败的原因:
async function registerForPushNotificationsAsync() { let token; try { 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.alert("必须启用通知以接收更新"); return null; } const pushTokenResult = await Notifications.getExpoPushTokenAsync(); token = pushTokenResult.data; console.log("Expo Push Token:", token); } else { Alert.alert("必须使用物理设备测试推送通知"); return null; } if (Platform.OS === "android") { await Notifications.setNotificationChannelAsync("default", { name: "default", importance: Notifications.AndroidImportance.MAX, vibrationPattern: [0, 250, 250, 250], lightColor: "#FF231F7C", }); } return token; } catch (error) { console.error("注册推送Token失败:", error); return null; } }
内容的提问来源于stack exchange,提问作者Nouman Khan
相关产品推荐
相关产品推荐

