Skip to content

Commit

Permalink
Try refreshing the FCM token if not found before trying to subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Nov 29, 2023
1 parent 9f98a83 commit 62aa04d
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/notifications/settings/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NOTIFICATIONS_API_KEY } from 'react-native-dotenv';
import { logger, RainbowError } from '@/logger';
import { RainbowNetworks } from '@/networks';
import {
NotificationSubscriptionWalletsType,
Expand Down Expand Up @@ -34,6 +35,8 @@ const updateNotificationSubscription = async (
Authorization: `Bearer ${NOTIFICATIONS_API_KEY}`,
},
});

// success
return {
error: false,
shouldRetry: false,
Expand All @@ -47,7 +50,10 @@ const updateNotificationSubscription = async (
};
}

// TODO JIN - sentry log this error
logger.error(new RainbowError('Failed to subscribe to notifications'), {
message: (error as Error).message,
});

return {
error: true,
shouldRetry: false,
Expand All @@ -68,6 +74,7 @@ const updateNotificationSubscriptionWithRetry = async (
// success
return true;
} else if (subscriptionResponse.shouldRetry) {
// retry with an updated FCM token
await saveFCMToken();
const refreshedFirebaseToken = await getFCMToken();
if (!refreshedFirebaseToken) return false;
Expand All @@ -76,35 +83,49 @@ const updateNotificationSubscriptionWithRetry = async (
refreshedFirebaseToken,
wallets
);
if (!subscriptionRetryResponse.error) {
return true;
} else {
return false;
}
return !subscriptionRetryResponse.error;
} else {
return false;
}
};

// returns updated wallet settings on success, undefined otherwise
export const publishWalletSettings = async (
walletSettings: WalletNotificationSettings[]
): Promise<WalletNotificationSettings[] | undefined> => {
const subscriptionPayload = parseWalletSettings(walletSettings);
const firebaseToken = await getFCMToken();
try {
const subscriptionPayload = parseWalletSettings(walletSettings);
let firebaseToken = await getFCMToken();

// refresh the FCM token if not found
if (!firebaseToken) {
await saveFCMToken();
firebaseToken = await getFCMToken();
if (!firebaseToken) return;
}

const success = await updateNotificationSubscriptionWithRetry(
firebaseToken,
subscriptionPayload
);
if (success) {
return walletSettings.map(setting => {
return {
...setting,
successfullyFinishedInitialSubscription: true,
};
});
} else {
return;
}
} catch (e: any) {
logger.error(
new RainbowError('Failed to publish wallet notification settings'),
{
message: (e as Error).message,
}
);

if (!firebaseToken) return;
const success = await updateNotificationSubscriptionWithRetry(
firebaseToken,
subscriptionPayload
);
if (success) {
return walletSettings.map(setting => {
return {
...setting,
successfullyFinishedInitialSubscription: true,
};
});
} else {
return;
}
};
Expand Down

0 comments on commit 62aa04d

Please sign in to comment.