From ccf1b4ba978a58def7e6e49ffebedfa2015c9848 Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 10 Jun 2024 14:09:55 -0700 Subject: [PATCH 1/5] stop using push token listener --- src/lib/notifications/notifications.ts | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 58aa4087c0..5a18e4f97a 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -43,7 +43,7 @@ async function getPushToken(skipPermissionCheck = false) { const granted = skipPermissionCheck || (await Notifications.getPermissionsAsync()).granted if (granted) { - Notifications.getDevicePushTokenAsync() + return Notifications.getDevicePushTokenAsync() } } @@ -56,17 +56,16 @@ export function useNotificationsRegistration() { return } - getPushToken() + // TEMPORARY. We want to use `Notifications.addPushTokenListener()` specifically on Android for this, since the token + // can change at points + ;(async () => { + const pushToken = await 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. - const subscription = Notifications.addPushTokenListener(async newToken => { - registerPushToken(agent, currentAccount, newToken) - }) + // If we don't get a push token back, it means that permission was denied + if (!pushToken) return - return () => { - subscription.remove() - } + registerPushToken(agent, currentAccount, pushToken) + })() }, [currentAccount, agent]) } @@ -107,11 +106,6 @@ export function useRequestNotificationsPermission() { context: context, status: res.status, }) - - if (res.granted) { - // This will fire a pushTokenEvent, which will handle registration of the token - getPushToken(true) - } } } From fa5e55fd44978684d29338e0d5dff167b8147799 Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 10 Jun 2024 14:36:19 -0700 Subject: [PATCH 2/5] cleanup the hack --- src/lib/notifications/notifications.ts | 38 ++++++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 5a18e4f97a..441d01ba0d 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) => @@ -56,16 +56,31 @@ export function useNotificationsRegistration() { return } - // TEMPORARY. We want to use `Notifications.addPushTokenListener()` specifically on Android for this, since the token - // can change at points - ;(async () => { - const pushToken = await getPushToken() + 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() + } - // If we don't get a push token back, it means that permission was denied - if (!pushToken) return + // 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. + const subscription = Notifications.addPushTokenListener(async newToken => { + registerPushToken(agent, currentAccount, { + data: newToken.data, + type: 'android', + }) + }) - registerPushToken(agent, currentAccount, pushToken) - })() + return () => { + subscription.remove() + } }, [currentAccount, agent]) } @@ -106,6 +121,11 @@ export function useRequestNotificationsPermission() { context: context, status: res.status, }) + + if (res.granted) { + // This will fire a pushTokenEvent, which will handle registration of the token + getPushToken(true) + } } } From c9b86a3e58ab32ee700d2781a3c48fd1b47e6a2b Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 10 Jun 2024 14:37:17 -0700 Subject: [PATCH 3/5] add a comment --- src/lib/notifications/notifications.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 441d01ba0d..c865b36c20 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -56,6 +56,10 @@ export function useNotificationsRegistration() { return } + // 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() From e1268e3bfaf704b52edc7952930532bf1271407b Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 10 Jun 2024 14:43:01 -0700 Subject: [PATCH 4/5] remove test code --- src/lib/notifications/notifications.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index c865b36c20..fba1fe32f3 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -76,10 +76,7 @@ export function useNotificationsRegistration() { // 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. const subscription = Notifications.addPushTokenListener(async newToken => { - registerPushToken(agent, currentAccount, { - data: newToken.data, - type: 'android', - }) + registerPushToken(agent, currentAccount, newToken) }) return () => { From 7420a7609c67408d9a04bc7eae226f8881c3b5a8 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 11 Jun 2024 02:06:16 +0200 Subject: [PATCH 5/5] Fix patch --- patches/expo-notifications+0.28.7.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patches/expo-notifications+0.28.7.patch b/patches/expo-notifications+0.28.7.patch index 3a9985c7b0..20f95185be 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