我目前正在开发 React Native Expo 应用程序,并且正在尝试使用 Expo 通知。它在 Expo Go 上运行良好,但构建 APK 后,我无法收到任何通知。谁能帮我? 这是我的 app.js 代码:
import "react-native-gesture-handler";
import { StatusBar, StyleSheet } from "react-native";
import Stacks from "./src/Routes/Stacks";
import { I18nManager } from "react-native";
import NoConnectionScreen from "./src/Seller Screens/Screens/NoConnectionScreen";
import { useNetInfo } from "@react-native-community/netinfo";
import { useEffect, useRef, useState } from "react";
import * as Device from "expo-device";
import * as Notifications from "expo-notifications";
import AsyncStorage from "@react-native-async-storage/async-storage";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: false,
}),
});
try {
I18nManager.allowRTL(false);
I18nManager.forceRTL(false);
// the next line is the most effective one
I18nManager.swapLeftAndRightInRTL(false);
} catch (e) {
console.log(e);
}
const App = () => {
state = { rtl: false };
const [isLoading, setIsLoading] = useState(true);
const notificationListener = useRef();
const responseListener = useRef();
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;
}
token = await Notifications.getExpoPushTokenAsync({
projectId: "b37ba3bc-286c-43a3-b3f2-ae2fa40ca6cd",
});
const expotoken = token.data;
console.log(token.data);
await AsyncStorage.setItem("expotoken", expotoken);
} else {
alert("Must use physical device for Push Notifications");
}
return token;
}
useEffect(() => {
registerForPushNotificationsAsync();
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
console.log(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log(response);
});
return () => {
Notifications.removeNotificationSubscription(
notificationListener.current
);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
setTimeout(() => {
setIsLoading(false);
}, 20);
const net = useNetInfo();
return (
<>
<StatusBar backgroundColor={"#6c757d"} barStyle="light-content" />
{net.isConnected == true && isLoading == false && <Stacks />}
{net.isConnected == false && <NoConnectionScreen />}
</>
);
};
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
尝试过 useNextNotificationsApi:true 和这个 展会推送:android:upload --api-key
权限设置是否正确?您的Android手机在第一次加载时是否会提示权限?
您必须确保您的SDK版本与您的Expo通知版本兼容。例如,我尝试将
"expo-notifications": "~0.28.9"
与 "expo": "51"
一起使用,它与 Expo 通知工具一起正常工作。
还要确保您的
app.json
文件中的通知设置正确:
"notification": {
"icon": "./assets/icon_notification.png",
"color": "#ffffff",
"iosDisplayInForeground": true,
"androidMode": "default"
}