Skip to content

Commit

Permalink
[C-2787] Reregister device-token on app startup (#3660)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Jun 27, 2023
1 parent ddfa510 commit 68e87ff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/common/src/services/audius-backend/AudiusBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ import {
Entity,
Achievement,
Notification,
IdentityNotification
IdentityNotification,
PushNotifications
} from '../../store'
import { CIDCache } from '../../store/cache/CIDCache'
import {
Expand Down Expand Up @@ -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
}
}

Expand Down
13 changes: 7 additions & 6 deletions packages/mobile/src/components/image/FastImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ export type ImageProps = Omit<FastImageProps, 'source'>
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 <RNFastImage source={imageSource} {...other} />
}
17 changes: 15 additions & 2 deletions packages/mobile/src/store/settings/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -157,6 +169,7 @@ function* watchRequestPushNotificationPermissions() {
export default function sagas() {
return [
...commonSettingsSagas(),
reregisterDeviceToken,
watchGetPushNotificationSettings,
watchUpdatePushNotificationSettings,
watchRequestPushNotificationPermissions
Expand Down

0 comments on commit 68e87ff

Please sign in to comment.