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

Expo 46打车应用:新订单通知时自动唤起App的需求

搞定Expo打车应用通知自动唤起APP的方案

要实现收到新订单通知时自动唤起APP,得从通知发送端参数配置和Expo客户端逻辑处理两方面来做:

1. 发送通知时必须加这些参数

通过Firebase发通知的时候,负载里得带上指定参数,不然APP没法自动响应:

  • Android端:在data字段里加click_action: "FLUTTER_NOTIFICATION_CLICK"(别管名字带Flutter,Expo完全兼容),同时把notification的priority设成high,确保通知是最高优先级。
  • iOS端:在apns.payload.aps里加content-available: 1,开启后台通知权限,这样后台收到通知能触发APP逻辑。

给你个参考的Firebase通知负载结构:

{
  "to": "你的Expo推送Token",
  "notification": {
    "title": "新订单来了",
    "body": "有用户叫车啦",
    "sound": "horn.wav"
  },
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "screen": "OrderScreen"
  },
  "android": {
    "priority": "high"
  },
  "apns": {
    "payload": {
      "aps": {
        "content-available": 1
      }
    }
  }
}

2. APP端加监听逻辑,收到通知就唤起

在你的App入口文件(比如App.js)里加通知监听器,不管是收到通知还是点击通知,都触发APP唤起和页面跳转:

import * as Notifications from 'expo-notifications';
import { Platform, Linking } from 'react-native';
import { useEffect } from 'react';
import { Navigation } from './navigation'; // 替换成你的导航实例

// 先配置通知的基础处理规则
Notifications.setNotificationHandler({
  handleNotification: async () => ({
    shouldShowAlert: true,
    shouldPlaySound: true,
    shouldSetBadge: false,
  }),
});

export default function App() {
  useEffect(() => {
    // 监听通知接收事件——后台收到通知直接唤起APP
    const receiveSub = Notifications.addNotificationReceivedListener(notification => {
      if (Platform.OS === 'android') {
        // Android用Linking打开APP指定页面
        Linking.openURL('your-app-scheme://orders'); // 替换成你的APP scheme和页面路径
      } else {
        // iOS直接跳转到订单页面
        Navigation.navigate('OrderScreen');
      }
    });

    // 监听通知点击事件——点击通知时唤起APP并跳转
    const clickSub = Notifications.addNotificationResponseReceivedListener(response => {
      Navigation.navigate('OrderScreen');
    });

    // 组件卸载时移除监听
    return () => {
      receiveSub.remove();
      clickSub.remove();
    };
  }, []);

  // ... 你的其他APP代码
}

3. Android额外配置

  • 把你代码里的bookings通知频道优先级改成Notifications.AndroidImportance.MAX,最高优先级的通知才能确保唤起APP:
    Notifications.setNotificationChannelAsync('bookings', {
      name: 'Booking notifications',
      sound: 'horn.wav',
      importance: Notifications.AndroidImportance.MAX, // 加上这行
    });
    
  • 在app.json里加intentFilters,让APP能响应通知的点击动作:
    {
      "expo": {
        "android": {
          "intentFilters": [
            {
              "action": "FLUTTER_NOTIFICATION_CLICK",
              "data": {
                "scheme": "your-app-scheme" // 替换成你的APP scheme
              },
              "category": ["android.intent.category.DEFAULT"]
            }
          ]
        }
      }
    }
    

4. iOS额外配置

  • 在app.json里开启后台通知权限:
    {
      "expo": {
        "ios": {
          "infoPlist": {
            "UIBackgroundModes": ["remote-notification"]
          }
        }
      }
    }
    
  • 还要确保用户给了后台刷新权限,可以在初始化的时候请求:
    import * as Permissions from 'expo-permissions'; // Expo 46用这个,新版本可能是expo-modules-core
    
    async function requestBackgroundPermission() {
      const { status } = await Permissions.askAsync(Permissions.BACKGROUND_FETCH);
      if (status !== 'granted') {
        console.log('后台刷新权限没拿到,通知可能没法自动唤起APP');
      }
    }
    

踩坑提醒

  • 必须用真机测试!模拟器没法完全模拟后台通知唤起的行为,白折腾。
  • Expo 46的expo-notifications版本要对应好,别乱升级,避免兼容性问题。
  • iOS的静默通知不会弹提示,要同时弹提示+唤起APP,必须在通知负载里加alert内容(就是你原来的title和body)。

内容的提问来源于stack exchange,提问作者juSt Chill

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:45:31