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

Expo安卓浮窗通知失效求助:仅状态栏显示通知

Expo安卓浮窗通知失效问题
  • 问题现象:通知可正常接收,但仅在状态栏显示,无法弹出浮窗
  • 当前使用版本:expo: ~44.0.0、expo-notifications: ~0.14.0
  • 配置与代码:代码完全复制自官方文档,已在app.json中添加"useNextNotificationsApi": true配置
  • 历史状态:使用expo: ~40.0.0、expo-notifications: ~0.8.2版本时,浮窗通知功能正常

相关代码

import * as app from 'app.json';
import * as Device from 'expo-device';
import { Subscription } from 'expo-modules-core';
import * as Notifications from 'expo-notifications';
import React, { useCallback, useEffect, useRef, useState } from 'react';

import { ScreenView } from '@shared/components/screenView';


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

export const Login: React.FC = () => {
  const notificationListener = useRef<unknown>();
  const responseListener = useRef<unknown>();

  const [token, setToken] = useState('');
  const [notification, setNotification] = useState<Notifications.Notification>();



  useEffect(() => {
    void registerForPushNotificationsAsync().then((token) => setToken(token ?? ''));
  }, []);


  useEffect(() => {
    notificationListener.current = Notifications.addNotificationReceivedListener((notification) => {
      setNotification(notification);
    });

    responseListener.current = Notifications.addNotificationResponseReceivedListener((response) => {
      console.log(response);
    });

  return () => {
      Notifications.removeNotificationSubscription(notificationListener.current);
      Notifications.removeNotificationSubscription(responseListener.current);
    };
  }, []);



  return (
    <ScreenView alignItems="center">
    </ScreenView>
  );
};


async function registerForPushNotificationsAsync() {
  let token;

  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('Failed to get push token for push notification!');
      return;
    }
    token = (await Notifications.getExpoPushTokenAsync()).data;
  } else {
    alert('Must use physical device for Push Notifications');
  }

  if (Platform.OS === 'android') {
    await Notifications.setNotificationChannelAsync('default', {
      name: 'default',
      importance: Notifications.AndroidImportance.MAX,
    });
  }

  return token;
}

相关截图

  • 状态栏显示:状态栏通知
  • 推送通知:推送通知

内容的提问来源于stack exchange,提问作者Anna Glebova

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 21:45:33