diff options
author | Hailey <me@haileyok.com> | 2024-06-10 18:16:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-11 03:16:21 +0200 |
commit | c73ad43a8005a90f8fe3152142cbed28c297ee6c (patch) | |
tree | a9b6e3828199f1ff469addb83c21b61c774fc905 | |
parent | 4e9a65200e2f355b020415a3a18ae1ade404635f (diff) | |
download | voidsky-c73ad43a8005a90f8fe3152142cbed28c297ee6c.tar.zst |
stop using `addPushTokenListener` (#4467)
* stop using push token listener * cleanup the hack * add a comment * remove test code * Fix patch --------- Co-authored-by: Dan Abramov <dan.abramov@gmail.com>
-rw-r--r-- | patches/expo-notifications+0.28.7.patch | 2 | ||||
-rw-r--r-- | src/lib/notifications/notifications.ts | 21 |
2 files changed, 19 insertions, 4 deletions
diff --git a/patches/expo-notifications+0.28.7.patch b/patches/expo-notifications+0.28.7.patch index 3a9985c7b..20f95185b 100644 --- a/patches/expo-notifications+0.28.7.patch +++ b/patches/expo-notifications+0.28.7.patch @@ -63,7 +63,7 @@ index f1fed19..80afe9e 100644 + @Nullable + public String getChannelId() { -+ return mTitle; ++ return mChannelId; + } + @Nullable diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 58aa4087c..fba1fe32f 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -6,7 +6,7 @@ import {BskyAgent} from '@atproto/api' import {logger} from '#/logger' import {SessionAccount, useAgent, useSession} from '#/state/session' import {logEvent, useGate} from 'lib/statsig/statsig' -import {devicePlatform, isNative} from 'platform/detection' +import {devicePlatform, isAndroid, isNative} from 'platform/detection' import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler' const SERVICE_DID = (serviceUrl?: string) => @@ -43,7 +43,7 @@ async function getPushToken(skipPermissionCheck = false) { const granted = skipPermissionCheck || (await Notifications.getPermissionsAsync()).granted if (granted) { - Notifications.getDevicePushTokenAsync() + return Notifications.getDevicePushTokenAsync() } } @@ -56,7 +56,22 @@ export function useNotificationsRegistration() { return } - getPushToken() + // HACK - see https://github.com/bluesky-social/social-app/pull/4467 + // An apparent regression in expo-notifications causes `addPushTokenListener` to not fire on Android whenever the + // token changes by calling `getPushToken()`. This is a workaround to ensure we register the token once it is + // generated on Android. + if (isAndroid) { + ;(async () => { + const token = await getPushToken() + + // Token will be undefined if we don't have notifications permission + if (token) { + registerPushToken(agent, currentAccount, token) + } + })() + } else { + getPushToken() + } // According to the Expo docs, there is a chance that the token will change while the app is open in some rare // cases. This will fire `registerPushToken` whenever that happens. |