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

Expo Notifications:本地通知无法触发BackgroundNotificationTask

解决Expo Notifications本地通知后台任务不触发的方案

一、先把BackgroundNotificationTask的配置拉满

  • 得在app.json里正确声明任务,名称要前后统一:
    {
      "expo": {
        "plugins": [
          [
            "expo-notifications",
            {
              "backgroundNotification": {
                "task": "my-background-notification-task"
              }
            }
          ]
        ]
      }
    }
    
  • 用TaskManager.defineTask注册任务时,名称必须和上面的一致:
    import * as TaskManager from 'expo-task-manager';
    import * as Notifications from 'expo-notifications';
    
    TaskManager.defineTask('my-background-notification-task', async ({ data, error }) => {
      if (error) {
        console.error('后台任务出错:', error);
        return;
      }
      if (data) {
        // 这里写你要在后台执行的代码
        console.log('拿到通知数据:', data);
      }
    });
    
  • App启动时一定要调用Notifications.registerTaskAsync完成任务注册,比如在App.js的useEffect里:
    useEffect(() => {
      async function setupTask() {
        await Notifications.registerTaskAsync('my-background-notification-task');
      }
      setupTask();
    }, []);
    

二、Android 13的权限不能漏

  • Android 13要求单独申请POST_NOTIFICATIONS权限,没这个权限通知都发不出去,更别说触发后台任务:
    import { Platform, PermissionsAndroid } from 'react-native';
    
    async function getNotificationPermission() {
      if (Platform.OS === 'android' && Platform.Version >= 33) {
        const granted = await PermissionsAndroid.request(
          PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
        );
        return granted === PermissionsAndroid.RESULTS.GRANTED;
      }
      return true;
    }
    
  • 调度通知前务必确认权限已经获取。

三、调度通知的参数别乱填

  • 用scheduleNotificationAsync时参数正常配置即可,比如:
    await Notifications.scheduleNotificationAsync({
      content: {
        title: '测试通知',
        body: '触发后台任务的通知',
      },
      trigger: {
        seconds: 10, // 10秒后触发,方便测试
      },
    });
    
  • 注意:如果把应用彻底划掉杀死,部分Android设备会直接终止后台任务,先测试App处于后台但未被杀死的情况。

四、实在不行换Foreground Service(仅Android)

如果BackgroundNotificationTask还是没反应,试试用前台服务——它优先级更高,不容易被系统回收:

  • 先安装依赖库:
    npx expo install expo-foreground-service
    
  • 在后台任务里启动前台服务:
    import * as ForegroundService from 'expo-foreground-service';
    
    TaskManager.defineTask('my-background-notification-task', async () => {
      await ForegroundService.startForegroundServiceAsync({
        id: 123,
        title: '后台任务运行中',
        body: '处理通知事件',
        icon: './assets/notification-icon.png', // 替换成你的图标路径
      });
    
      // 执行你的后台逻辑,比如请求接口、存储数据
    
      await ForegroundService.stopForegroundServiceAsync();
    });
    
  • 还要在app.json中添加权限:
    {
      "expo": {
        "android": {
          "permissions": ["FOREGROUND_SERVICE"]
        }
      }
    }
    

五、调试技巧

  • 用adb logcat查看Android日志,过滤Expo相关内容,能快速定位问题:
    adb logcat | grep ExpoTaskManager
    
  • 必须用EAS构建的包测试,Expo Go沙盒环境不支持后台任务这类功能。

内容的提问来源于stack exchange,提问作者Geeoon Chung

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 08:28:49