Я следовал тому, что сказано в документации. Я могу получать и отправлять уведомления. Проблема в том, что уведомления не появлялись (только звук), когда мое приложение работало в фоновом режиме или полностью закрывалось. Всплывающее окно работает только тогда, когда я открываю приложение (на переднем плане). Звук уведомления работает нормально, когда приложение находится во всех состояниях. Я использую инструмент push-уведомлений Expo и свой сервер для отправки уведомлений.
Что здесь не так с моим кодом?
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
async function registerForPushNotificationsAsync() {
let token;
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
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;
}
// Learn more about projectId:
// https://docs.expo.dev/push-notification ... -projectid
token = (
await Notifications.getExpoPushTokenAsync({
projectId: Constants.expoConfig.extra.eas.projectId,
})
).data;
console.log(token);
} else {
alert("Must use physical device for Push Notifications");
}
return token;
}
useEffect(() => {
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
// setNotification(notification);
notificationRef.current = notification;
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log(JSON.stringify(response, null, 2));
console.log("DATA", response?.notification.request.content.data);
});
return () => {
Notifications.removeNotificationSubscription(
notificationListener.current
);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
Подробнее здесь: https://stackoverflow.com/questions/782 ... etely-clos
Почему уведомления о выставках не появляются, когда приложение находится в фоновом режиме/полностью закрыто? (android) ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение