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
相关产品推荐
相关产品推荐

