Completely reworked the app by starting fresh
- Now you can just swipe to access categories - Redone side menu - Inital API input screen is more legible - A goose as an icon - Proper display and internal name - Correct handling of incoming data
This commit is contained in:
1 parent
8895bc9c9d
commit
26e5b0ae6f
26 files changed
+6089
-1810
No files matched your search
@@ -2,6 +2,7 @@ import * as Device from "expo-device";
|
||||
import * as Notifications from "expo-notifications";
|
||||
import { useEffect, useState } from "react";
|
||||
import { Platform } from "react-native";
|
||||
import { version as packageVersion } from '../package.json';
|
||||
|
||||
type RegisterTokenParams = {
|
||||
apiKey?: string | null;
|
||||
@@ -14,46 +15,104 @@ type RegisterTokenParams = {
|
||||
export function usePushNotifications({
|
||||
apiKey,
|
||||
backendUrl,
|
||||
appVersion = "1.0.0",
|
||||
appVersion = packageVersion,
|
||||
locale,
|
||||
topics
|
||||
}: RegisterTokenParams & { apiKey?: string | null }) {
|
||||
const [expoPushToken, setExpoPushToken] = useState<string | null>(null);
|
||||
const [registered, setRegistered] = useState(false);
|
||||
const [registrationError, setRegistrationError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!apiKey) return;
|
||||
if (!apiKey) {
|
||||
console.log("No API key available, skipping registration");
|
||||
return;
|
||||
}
|
||||
|
||||
const registerTokenWithBackend = async (token: string) => {
|
||||
console.log("Attempting to register token with backend...");
|
||||
const res = await fetch(`${backendUrl}/register-token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-API-Key": apiKey
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
platform: Platform.OS,
|
||||
app_ver: appVersion,
|
||||
locale,
|
||||
topics
|
||||
})
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
console.error("Backend registration failed:", res.status, errorText);
|
||||
throw new Error(`Failed to register token: ${res.status}`);
|
||||
}
|
||||
|
||||
console.log("Push token registered with backend ✅");
|
||||
};
|
||||
|
||||
(async () => {
|
||||
const token = await registerForPushNotificationsAsync();
|
||||
if (token) {
|
||||
setExpoPushToken(token);
|
||||
await registerTokenWithBackend(token);
|
||||
try {
|
||||
const token = await registerForPushNotificationsAsync();
|
||||
if (token) {
|
||||
console.log("Got push token:", token);
|
||||
setExpoPushToken(token);
|
||||
|
||||
await registerTokenWithBackend(token);
|
||||
setRegistered(true);
|
||||
setRegistrationError(null);
|
||||
} else {
|
||||
console.log("No push token received");
|
||||
setRegistrationError("Could not get push token");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Registration error:", err);
|
||||
setRegistered(false);
|
||||
setRegistrationError(err instanceof Error ? err.message : "Failed to register push token");
|
||||
}
|
||||
})();
|
||||
}, [apiKey]);
|
||||
}, [apiKey, backendUrl, appVersion, locale, topics]);
|
||||
|
||||
const unregisterToken = async () => {
|
||||
if (!expoPushToken || !apiKey) {
|
||||
console.log("Cannot unregister: missing token or API key");
|
||||
return;
|
||||
}
|
||||
|
||||
const registerTokenWithBackend = async (token: string) => {
|
||||
try {
|
||||
await fetch(`${backendUrl}/register_token`, {
|
||||
console.log("Unregistering token from backend...");
|
||||
const res = await fetch(`${backendUrl}/unregister-token`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-API-Key": apiKey ?? "",
|
||||
"X-API-Key": apiKey,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
token,
|
||||
token: expoPushToken,
|
||||
platform: Platform.OS,
|
||||
app_ver: appVersion,
|
||||
locale,
|
||||
topics
|
||||
})
|
||||
});
|
||||
console.log("Push token registered with backend ✅");
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`Unregister failed: ${res.status}`);
|
||||
}
|
||||
|
||||
console.log("Push token unregistered from backend ✅");
|
||||
setExpoPushToken(null);
|
||||
setRegistered(false);
|
||||
} catch (error) {
|
||||
console.error("Failed to register token with backend:", error);
|
||||
console.error("Failed to unregister token:", error);
|
||||
}
|
||||
};
|
||||
|
||||
return expoPushToken;
|
||||
return { expoPushToken, registered, registrationError, unregisterToken };
|
||||
}
|
||||
|
||||
async function registerForPushNotificationsAsync() {
|
||||
@@ -90,4 +149,4 @@ async function registerForPushNotificationsAsync() {
|
||||
}
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user