通知会出现,但只显示在状态栏中。
我使用“世博”:"~44.0.0“和”博览会-通知“:"~0.14.0”。
代码是从文档中完全复制的。
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;
}
我还添加到app.json中。
"useNextNotificationsApi": true
以前的“博览会”版本:"~40.0.0“和”博览会-通知“:"~0.8.2”,它起作用了。
发布于 2022-07-25 09:16:21
如果应用程序被打开,您需要实现应用程序中的通知。查看一下:https://docs.expo.dev/versions/latest/sdk/notifications/
https://stackoverflow.com/questions/73106105
复制相似问题