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,10 +1,10 @@
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import * as Notifications from "expo-notifications";
|
||||
import { useEffect } from "react";
|
||||
import { Category, categoryKeys } from "types/Category";
|
||||
import { Item } from "types/Item";
|
||||
import { Category, categoryKeys } from "../types/Category";
|
||||
import { Item } from "../types/Item";
|
||||
|
||||
const STORAGE_KEY = "notifications-data";
|
||||
export const STORAGE_KEY = "notifications-data";
|
||||
|
||||
// Hook: listens for incoming notifications → Item + persists them
|
||||
export function useNotificationListener(onItems: (items: Item[]) => void) {
|
||||
@@ -48,27 +48,29 @@ export function useNotificationListener(onItems: (items: Item[]) => void) {
|
||||
}
|
||||
|
||||
// Convert Expo notification → Item
|
||||
function mapNotificationToItem(notification: Notifications.Notification): Item | null {
|
||||
export function mapNotificationToItem(notification: Notifications.Notification): Item | null {
|
||||
try {
|
||||
const content = notification.request.content;
|
||||
const rawCategory = content.data?.category as Category | undefined;
|
||||
const category: Category = rawCategory && categoryKeys.includes(rawCategory) ? rawCategory : "home";
|
||||
const data = content.data || {};
|
||||
|
||||
const rawCategory = data.category as string | undefined;
|
||||
const category: Category = categoryKeys.includes(rawCategory as Category)
|
||||
? (rawCategory as Category)
|
||||
: "utility";
|
||||
|
||||
return {
|
||||
timestamp: Date.now(), // could use backend timestamp if provided
|
||||
timestamp: data.timestamp ? Number(data.timestamp) : Date.now(),
|
||||
category,
|
||||
title: content.title ?? "No title",
|
||||
info: content.body ?? "No details",
|
||||
link: (content.data?.link as string) ?? "",
|
||||
info: content.body ?? "No description",
|
||||
link: typeof data.link === "string" ? data.link : "#",
|
||||
};
|
||||
} catch (e) {
|
||||
console.error("Failed to map notification to Item:", e);
|
||||
} catch (err) {
|
||||
console.error("Failed to map notification to Item:", err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export { mapNotificationToItem };
|
||||
|
||||
// Save a new item into storage and update state
|
||||
async function addItem(item: Item, onItems: (items: Item[]) => void) {
|
||||
try {
|
||||
@@ -83,3 +85,5 @@ async function addItem(item: Item, onItems: (items: Item[]) => void) {
|
||||
console.error("Failed to persist notification:", e);
|
||||
}
|
||||
}
|
||||
|
||||
export default null;
|
||||
@@ -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