我遇到的问题是使用世博推送通知API发送推送通知。我正在向Expo推送通知V2 API发送HTTP POST请求,向Expo推送令牌发送通知,该令牌是设备在Expo注册获得的。问题是通知只出现在前台而不是后台。但是,我使用Postman和expo推送通知工具测试发送通知时,它们会出现在后台。
我已经检查了在后台处理通知所需的权限和代码是否到位,并且设备设置是否允许在后台处理通知。我还仔细检查了我的应用程序中Expo推送通知API的实现,以确保我向Expo服务器发送了正确的数据,并且正确地处理了响应。此外,我关闭了电池保护程序,也尝试了其他手机,但都是徒劳的,我还建立了一个独立的apk,但同样的问题与那一个也。
我已经遵循此指南https://docs.expo.dev/push-notifications/push-notifications-setup/
我正在寻求帮助,以了解为什么从我的Expo应用程序发送通知时没有出现在后台,但当我使用Postman和Expo推送通知工具测试它们时会出现。我将感谢任何关于如何解决此问题的指导或建议。
这是我的App.js代码:
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") {
Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: "#FF231F7C",
});
}
return token;
}
async function sendPushNotification(
expoPushToken: any,
type?: string,
name?: string
) {
const message = {
to: expoPushToken,
sound: "default",
title: "HI",
body: "BY",
};
await fetch("https://exp.host/--/api/v2/push/send", {
method: "POST",
headers: {
Accept: "application/json",
"Accept-encoding": "gzip, deflate",
"Content-Type": "application/json",
},
body: JSON.stringify(message),
});
}
export default function App() {
const { isLoadingComplete, user, error, refresh } = useCachedResources();
const [notifications, setNotifications] = useState<Notification[]>([]);
const [expoPushToken, setExpoPushToken] = useState<any>();
const [notification, setNotification] = useState<any>(false);
const notificationListener = useRef<any>();
const responseListener = useRef<any>();
useEffect(() => {
registerForPushNotificationsAsync().then((token) =>
setExpoPushToken(token)
);
console.log("eee", expoPushToken);
notificationListener.current =
Notifications.addNotificationReceivedListener((notification) => {
setNotification(notification);
});
responseListener.current =
Notifications.addNotificationResponseReceivedListener(
async (response) => {
await Notifications.setBadgeCountAsync(0);
navRef.navigate("Dashboard", {
screen: "Messages",
});
}
);
return () => {
Notifications.removeNotificationSubscription(
notificationListener.current
);
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
useEffect(() => {
const newNotifications = notifications?.filter(
(not) => not.status === "new"
);
newNotifications?.forEach(async (not) => {
await sendPushNotification(expoPushToken, not.type, not.by.name);
updateNotification(not.id!!, {
status: "old",
});
});
}, [notifications]);
useEffect(() => {
// getFCMToken();
let unsubscribeNotification: Unsubscribe;
if (user && user.id) {
unsubscribeNotification = subscribeNotification(
user.id,
handleNotificationChange
);
}
return () => {
if (unsubscribeNotification) {
unsubscribeNotification();
}
};
}, [user]);
const handleNotificationChange = (snapshot: QuerySnapshot<Notification>) => {
let result: Notification[] = [];
snapshot.forEach((doc) => {
result.push({ ...doc.data(), id: doc.id });
});
setNotifications(result);
};
字符串
1条答案
按热度按时间nmpmafwu1#
我有同样的问题,我只是删除了通道代码,它开始工作,按照文件,如果我们设置通道,那么我们将不得不发送channelId请求。
字符串