You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Expo本地通知在iOS设备无法弹出问题求助

解决iOS上Expo本地通知不弹出的问题

你的判断完全正确,问题根源就是未获取通知权限——iOS对通知权限管控严格,必须先获得用户授权,通知才能正常触发。下面是修改后的完整代码,以及关键注意点:

修改后的App.js代码

import { StyleSheet, Button, View, Alert } from 'react-native';
import * as Notifications from 'expo-notifications';

Notifications.setNotificationHandler({
  handleNotification: async () => {
    return {
      shouldPlaySound: false,
      shouldSetBadge: false,
      shouldShowAlert: true,
    };
  }
});

export default function App() {
  async function scheduleNotificationHandler() {
    // 1. 检查当前权限状态
    const permissionStatus = await Notifications.getPermissionsAsync();
    let hasPermission = permissionStatus.granted;

    // 2. 未授权则请求权限
    if (!hasPermission) {
      const requestResult = await Notifications.requestPermissionsAsync();
      hasPermission = requestResult.granted;
    }

    // 3. 仅在授权后调度通知
    if (hasPermission) {
      await Notifications.scheduleNotificationAsync({
        content: {
          title: 'My first local notification',
          body: 'This is the body of the notification.',
        },
        trigger: {
          seconds: 5
        }
      });
    } else {
      Alert.alert('权限不足', '请开启通知权限以接收通知');
    }
  }

  return (
    <View style={styles.container}>
      <Button
        title="Schedule Notification"
        onPress={scheduleNotificationHandler}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

关键说明

  • 权限检查与请求:每次触发通知前先检查权限,未授权时主动请求,这是iOS通知功能的必要前提。
  • 权限拒绝后的提示:如果用户拒绝权限,用弹窗提示用户去系统设置里开启,避免无反馈的静默失败。

额外排查点

  • 检查设备「设置」-> 你的App ->「通知」,确认已开启允许通知,并且「横幅样式」等显示选项正常配置。
  • 测试时可以将App退到后台,部分iOS版本在App前台时通知可能不会弹出横幅(虽然代码中shouldShowAlert设为true前台应该会弹,但后台测试更稳妥)。
  • 确保你的Expo Go是最新版本,避免因版本兼容性导致的API失效。

内容的提问来源于stack exchange,提问作者Luiz Felipe Nast Roballo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:40:42