Drill into notifications handlers
(cherry picked from commit 7ac9e500866732e1f2e205bbe96e70db331e5ffb)
This commit is contained in:
@@ -1,12 +1,13 @@
|
|||||||
import {useEffect} from 'react'
|
import {useEffect} from 'react'
|
||||||
import * as Notifications from 'expo-notifications'
|
import * as Notifications from 'expo-notifications'
|
||||||
|
import {BskyAgent} from '@atproto/api'
|
||||||
import {QueryClient} from '@tanstack/react-query'
|
import {QueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed'
|
import {RQKEY as RQKEY_NOTIFS} from '#/state/queries/notifications/feed'
|
||||||
import {invalidateCachedUnreadPage} from '#/state/queries/notifications/unread'
|
import {invalidateCachedUnreadPage} from '#/state/queries/notifications/unread'
|
||||||
import {truncateAndInvalidate} from '#/state/queries/util'
|
import {truncateAndInvalidate} from '#/state/queries/util'
|
||||||
import {getAgent, SessionAccount} from '#/state/session'
|
import {SessionAccount} from '#/state/session'
|
||||||
import {track} from 'lib/analytics/analytics'
|
import {track} from 'lib/analytics/analytics'
|
||||||
import {devicePlatform, isIOS} from 'platform/detection'
|
import {devicePlatform, isIOS} from 'platform/detection'
|
||||||
import {resetToTab} from '../../Navigation'
|
import {resetToTab} from '../../Navigation'
|
||||||
@@ -18,6 +19,7 @@ const SERVICE_DID = (serviceUrl?: string) =>
|
|||||||
: 'did:web:api.bsky.app'
|
: 'did:web:api.bsky.app'
|
||||||
|
|
||||||
export async function requestPermissionsAndRegisterToken(
|
export async function requestPermissionsAndRegisterToken(
|
||||||
|
agent: BskyAgent,
|
||||||
account: SessionAccount,
|
account: SessionAccount,
|
||||||
) {
|
) {
|
||||||
// request notifications permission once the user has logged in
|
// request notifications permission once the user has logged in
|
||||||
@@ -29,7 +31,7 @@ export async function requestPermissionsAndRegisterToken(
|
|||||||
// register the push token with the server
|
// register the push token with the server
|
||||||
const token = await Notifications.getDevicePushTokenAsync()
|
const token = await Notifications.getDevicePushTokenAsync()
|
||||||
try {
|
try {
|
||||||
await getAgent().api.app.bsky.notification.registerPush({
|
await agent.api.app.bsky.notification.registerPush({
|
||||||
serviceDid: SERVICE_DID(account.service),
|
serviceDid: SERVICE_DID(account.service),
|
||||||
platform: devicePlatform,
|
platform: devicePlatform,
|
||||||
token: token.data,
|
token: token.data,
|
||||||
@@ -49,6 +51,7 @@ export async function requestPermissionsAndRegisterToken(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function registerTokenChangeHandler(
|
export function registerTokenChangeHandler(
|
||||||
|
agent: BskyAgent,
|
||||||
account: SessionAccount,
|
account: SessionAccount,
|
||||||
): () => void {
|
): () => void {
|
||||||
// listens for new changes to the push token
|
// listens for new changes to the push token
|
||||||
@@ -60,7 +63,7 @@ export function registerTokenChangeHandler(
|
|||||||
logger.DebugContext.notifications,
|
logger.DebugContext.notifications,
|
||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
await getAgent().api.app.bsky.notification.registerPush({
|
await agent.api.app.bsky.notification.registerPush({
|
||||||
serviceDid: SERVICE_DID(account.service),
|
serviceDid: SERVICE_DID(account.service),
|
||||||
platform: devicePlatform,
|
platform: devicePlatform,
|
||||||
token: newToken.data,
|
token: newToken.data,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import * as NavigationBar from 'expo-navigation-bar'
|
|||||||
import {StatusBar} from 'expo-status-bar'
|
import {StatusBar} from 'expo-status-bar'
|
||||||
import {useNavigationState} from '@react-navigation/native'
|
import {useNavigationState} from '@react-navigation/native'
|
||||||
|
|
||||||
import {useSession} from '#/state/session'
|
import {getAgent, useSession} from '#/state/session'
|
||||||
import {
|
import {
|
||||||
useIsDrawerOpen,
|
useIsDrawerOpen,
|
||||||
useIsDrawerSwipeDisabled,
|
useIsDrawerSwipeDisabled,
|
||||||
@@ -78,8 +78,14 @@ function ShellInner() {
|
|||||||
// only runs when did changes
|
// only runs when did changes
|
||||||
if (currentAccount && currentAccountDid.current !== currentAccount.did) {
|
if (currentAccount && currentAccountDid.current !== currentAccount.did) {
|
||||||
currentAccountDid.current = currentAccount.did
|
currentAccountDid.current = currentAccount.did
|
||||||
notifications.requestPermissionsAndRegisterToken(currentAccount)
|
notifications.requestPermissionsAndRegisterToken(
|
||||||
const unsub = notifications.registerTokenChangeHandler(currentAccount)
|
getAgent(),
|
||||||
|
currentAccount,
|
||||||
|
)
|
||||||
|
const unsub = notifications.registerTokenChangeHandler(
|
||||||
|
getAgent(),
|
||||||
|
currentAccount,
|
||||||
|
)
|
||||||
return unsub
|
return unsub
|
||||||
}
|
}
|
||||||
}, [currentAccount])
|
}, [currentAccount])
|
||||||
|
|||||||
Reference in New Issue
Block a user