Skip to content

Commit

Permalink
feat: prompt for notifications if not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Jan 16, 2025
1 parent b390f06 commit 57b7567
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
11 changes: 11 additions & 0 deletions app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { NAV_THEME } from "~/lib/constants";
import { isBiometricSupported } from "~/lib/isBiometricSupported";
import { useAppStore } from "~/lib/state/appStore";
import { useColorScheme } from "~/lib/useColorScheme";
import { registerForPushNotificationsAsync } from "~/services/Notifications";

const LIGHT_THEME: Theme = {
...DefaultTheme,
Expand Down Expand Up @@ -68,6 +69,15 @@ export default function RootLayout() {
}
}

async function checkAndPromptForNotifications() {
const isEnabled = useAppStore.getState().isNotificationsEnabled;
// prompt the user to enable notifications on first open
if (isEnabled === null) {
const enabled = await registerForPushNotificationsAsync();
useAppStore.getState().setNotificationsEnabled(enabled);
}
}

const loadTheme = React.useCallback((): Promise<void> => {
return new Promise((resolve) => {
const theme = useAppStore.getState().theme;
Expand All @@ -86,6 +96,7 @@ export default function RootLayout() {
await Promise.all([loadTheme(), loadFonts(), checkBiometricStatus()]);
} finally {
setResourcesLoaded(true);
await checkAndPromptForNotifications();
SplashScreen.hide();
}
};
Expand Down
15 changes: 10 additions & 5 deletions lib/state/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface AppState {
readonly wallets: Wallet[];
readonly addressBookEntries: AddressBookEntry[];
readonly isSecurityEnabled: boolean;
readonly isNotificationsEnabled: boolean;
readonly isNotificationsEnabled: boolean | null;
readonly isOnboarded: boolean;
readonly expoPushToken: string;
readonly theme?: Theme;
Expand All @@ -28,7 +28,7 @@ interface AppState {
setFiatCurrency(fiatCurrency: string): void;
setSelectedWalletId(walletId: number): void;
setSecurityEnabled(securityEnabled: boolean): void;
setNotificationsEnabled(notificationsEnabled: boolean): void;
setNotificationsEnabled(notificationsEnabled: boolean | null): void;
addWallet(wallet: Wallet): void;
addAddressBookEntry(entry: AddressBookEntry): void;
removeAddressBookEntry: (index: number) => void;
Expand Down Expand Up @@ -196,8 +196,9 @@ export const useAppStore = create<AppState>()((set, get) => {
nwcClient: getNWCClient(initialSelectedWalletId),
fiatCurrency: secureStorage.getItem(fiatCurrencyKey) || "",
isSecurityEnabled,
isNotificationsEnabled:
secureStorage.getItem(isNotificationsEnabledKey) === "true",
isNotificationsEnabled: secureStorage.getItem(isNotificationsEnabledKey)
? secureStorage.getItem(isNotificationsEnabledKey) === "true"
: null,
theme,
balanceDisplayMode,
isOnboarded: secureStorage.getItem(hasOnboardedKey) === "true",
Expand Down Expand Up @@ -239,7 +240,11 @@ export const useAppStore = create<AppState>()((set, get) => {
});
},
setNotificationsEnabled: (isEnabled) => {
secureStorage.setItem(isNotificationsEnabledKey, isEnabled.toString());
if (isEnabled === null) {
secureStorage.removeItem(isNotificationsEnabledKey);
} else {
secureStorage.setItem(isNotificationsEnabledKey, isEnabled.toString());
}
set({
isNotificationsEnabled: isEnabled,
});
Expand Down
9 changes: 5 additions & 4 deletions pages/settings/Notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ export function Notifications() {
</Label>
<Switch
disabled={isLoading}
checked={isEnabled}
checked={!!isEnabled}
onCheckedChange={async (checked) => {
setLoading(true);
if (checked) {
checked = await registerForPushNotificationsAsync();
let enabled: boolean | null = checked;
if (enabled) {
enabled = await registerForPushNotificationsAsync();
} else {
const wallets = useAppStore.getState().wallets;
for (const wallet of wallets) {
Expand All @@ -35,7 +36,7 @@ export function Notifications() {
await removeAllInfo();
useAppStore.getState().setExpoPushToken("");
}
useAppStore.getState().setNotificationsEnabled(checked);
useAppStore.getState().setNotificationsEnabled(enabled);
setLoading(false);
}}
nativeID="security"
Expand Down
17 changes: 10 additions & 7 deletions services/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { errorToast } from "~/lib/errorToast";
import { registerWalletNotifications } from "~/lib/notifications";
import { useAppStore } from "~/lib/state/appStore";

export async function registerForPushNotificationsAsync(): Promise<boolean> {
export async function registerForPushNotificationsAsync(): Promise<
boolean | null
> {
if (Platform.OS === "android") {
ExpoNotifications.setNotificationChannelAsync("default", {
name: "default",
Expand All @@ -27,12 +29,13 @@ export async function registerForPushNotificationsAsync(): Promise<boolean> {
const { status } = await ExpoNotifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== "granted") {
errorToast(
new Error(
"Permission not granted to get push token for push notification",
),
);
if (finalStatus === "undetermined") {
return null;
}
if (finalStatus === "denied") {
if (existingStatus === "denied") {
errorToast(new Error("Enable app notifications in device settings"));
}
return false;
}
const projectId =
Expand Down

0 comments on commit 57b7567

Please sign in to comment.