diff --git a/packages/common/src/services/audius-backend/AudiusBackend.ts b/packages/common/src/services/audius-backend/AudiusBackend.ts index acb78b07598..80e62f5b4b3 100644 --- a/packages/common/src/services/audius-backend/AudiusBackend.ts +++ b/packages/common/src/services/audius-backend/AudiusBackend.ts @@ -64,7 +64,8 @@ import { Entity, Achievement, Notification, - IdentityNotification + IdentityNotification, + PushNotifications } from '../../store' import { CIDCache } from '../../store/cache/CIDCache' import { @@ -2812,9 +2813,10 @@ export const audiusBackend = ({ } }) .then((res) => res.json()) - .then((res) => res.settings) + .then((res: { settings: PushNotifications }) => res.settings) } catch (e) { console.error(e) + return null } } diff --git a/packages/mobile/src/components/image/FastImage.tsx b/packages/mobile/src/components/image/FastImage.tsx index 28f27d0fd2a..95a1e751643 100644 --- a/packages/mobile/src/components/image/FastImage.tsx +++ b/packages/mobile/src/components/image/FastImage.tsx @@ -15,12 +15,13 @@ export type ImageProps = Omit export const FastImage = (props: FastImageProps) => { const { source, priority, ...other } = props - const imageSource = - typeof source === 'number' - ? source - : Array.isArray(source) - ? { uri: source[0].uri, priority } - : { uri: source.uri, priority } + const imageSource = !source + ? source + : typeof source === 'number' + ? source + : Array.isArray(source) + ? { uri: source[0].uri, priority } + : { uri: source.uri, priority } return } diff --git a/packages/mobile/src/store/settings/sagas.ts b/packages/mobile/src/store/settings/sagas.ts index adfcfb02c89..6a20ef88b45 100644 --- a/packages/mobile/src/store/settings/sagas.ts +++ b/packages/mobile/src/store/settings/sagas.ts @@ -26,6 +26,18 @@ export function* deregisterPushNotifications() { yield* call(audiusBackendInstance.deregisterDeviceToken, token) } +/* + * Runs once at startup and re-registers the device-token in case it changes + */ +function* reregisterDeviceToken() { + const audiusBackend = yield* getContext('audiusBackendInstance') + const hasPermission = yield* call([PushNotifications, 'hasPermission']) + if (hasPermission) { + const { token, os } = yield* call([PushNotifications, 'getToken']) + yield* call(audiusBackend.registerDeviceToken, token, os) + } +} + function* enablePushNotifications() { yield* call([PushNotifications, 'requestPermission']) const { token, os } = yield* call([PushNotifications, 'getToken']) @@ -71,9 +83,9 @@ function* watchGetPushNotificationSettings() { yield* takeEvery(actions.GET_PUSH_NOTIFICATION_SETTINGS, function* () { yield* call(waitForRead) try { - const settings = (yield* call( + const settings = yield* call( audiusBackendInstance.getPushNotificationSettings - )) as TPushNotifications + ) let pushNotificationSettings = mapValues( initialState.pushNotifications, function (_val: boolean) { @@ -157,6 +169,7 @@ function* watchRequestPushNotificationPermissions() { export default function sagas() { return [ ...commonSettingsSagas(), + reregisterDeviceToken, watchGetPushNotificationSettings, watchUpdatePushNotificationSettings, watchRequestPushNotificationPermissions