Skip to content

Commit

Permalink
stop using addPushTokenListener (#4467)
Browse files Browse the repository at this point in the history
* stop using push token listener

* cleanup the hack

* add a comment

* remove test code

* Fix patch

---------

Co-authored-by: Dan Abramov <[email protected]>
  • Loading branch information
haileyok and gaearon authored Jun 11, 2024
1 parent 4e9a652 commit c73ad43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion patches/expo-notifications+0.28.7.patch
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ index f1fed19..80afe9e 100644

+ @Nullable
+ public String getChannelId() {
+ return mTitle;
+ return mChannelId;
+ }
+
@Nullable
Expand Down
21 changes: 18 additions & 3 deletions src/lib/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -43,7 +43,7 @@ async function getPushToken(skipPermissionCheck = false) {
const granted =
skipPermissionCheck || (await Notifications.getPermissionsAsync()).granted
if (granted) {
Notifications.getDevicePushTokenAsync()
return Notifications.getDevicePushTokenAsync()
}
}

Expand All @@ -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.
Expand Down

0 comments on commit c73ad43

Please sign in to comment.