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

React Native Expo如何实现类WhatsApp的悬浮抬头通知

修复方案

按以下步骤调整即可触发Heads-up悬浮弹窗通知:

  • 首先处理安卓通知通道的系统级限制:安卓通知通道首次创建后,后续代码修改importance、震动等配置不会生效。先卸载测试设备上的应用重装,或清除应用全部数据,保证高优先级通道能被正确创建。所有通知通道初始化方法必须加await,避免通道未创建完成时通知被发送到默认低优先级通道。
  • 补全通知配置,修正错误项:
    不要用字符串形式的优先级配置,统一使用expo-notifications提供的枚举值,同时开启前台横幅权限:
// 通道初始化必须await
await Notifications.setNotificationChannelAsync('location-reached', {
    name: 'location-reached',
    importance: Notifications.AndroidImportance.MAX,
    vibrationPattern: [0, 250, 250, 250],
    lightColor: '#FF231F7C',
});

Notifications.setNotificationHandler({
    handleNotification: async () => ({
        shouldShowAlert: true,
        shouldPlaySound: true,
        shouldSetBadge: true,
        shouldShowBanner: true, // 新增:前台状态允许弹出悬浮横幅
        priority: Notifications.AndroidNotificationPriority.MAX, // 替换原字符串"high"
    }),
});

发送通知时在content中显式声明优先级,避免配置不生效:

export const sendNotification = async (address) => {
    // 每次发通知前可重复调用通道初始化方法,已创建的同名通道不会重复生成,可保证配置一致性
    await Notifications.setNotificationChannelAsync('location-reached', {
        name: 'location-reached',
        importance: Notifications.AndroidImportance.MAX,
        vibrationPattern: [0, 250, 250, 250],
        lightColor: '#FF231F7C',
    });

    const id = await Notifications.scheduleNotificationAsync( {
        content: {
            title: `Destination will arrive soon`,
            body: address,
            sound: 'default',
            vibrate: true,
            priority: Notifications.AndroidNotificationPriority.MAX, // 新增显式优先级配置
            visibility: Notifications.AndroidNotificationVisibility.PUBLIC, // 可选:锁屏状态下也可显示通知内容
            // 如需添加操作按钮,在此配置actions数组即可
            // actions: [
            //     { identifier: 'confirm', buttonTitle: '确认', options: { opensAppToForeground: true } },
            //     { identifier: 'dismiss', buttonTitle: '忽略', options: { isDestructive: true } }
            // ]
        },
        trigger: {
            seconds: 1,
            channelId: 'location-reached'
        },
    });
    console.log("notif id on scheduling", id)
    return id;
}
  • 检查国产ROM自定义权限:小米、华为、OPPO、vivo等国产定制安卓系统会默认拦截第三方应用的悬浮通知,需要手动进入应用通知设置页,找到对应location-reached通知分类,手动打开「悬浮通知/横幅通知」开关,代码配置无法绕过该系统限制。

内容的提问来源于stack exchange,提问作者Ricky Anwar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 23:09:24