Skip to content

Commit

Permalink
fix: notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
salmanm committed Oct 21, 2021
1 parent 73eb191 commit 026c6f1
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/screens/HomeScreen.tsx
Expand Up @@ -3,6 +3,7 @@ import * as Notifications from 'expo-notifications'
import { StyleSheet, ScrollView, Alert } from 'react-native'
import { Subscription } from '@unimodules/react-native-adapter'
import { StackNavigationProp } from '@react-navigation/stack'
import { NotificationResponse } from 'expo-notifications'

import { useSecrets } from '../context/SecretsContext'
import { useAuth } from '../context/AuthContext'
Expand All @@ -14,6 +15,11 @@ import usePushToken from '../hooks/use-push-token'
import { Secret } from '../types'
import { MainStackParamList } from '../Main'

type NotificationData = {
secretId: string
uniqueId: string
}

const styles = StyleSheet.create({
container: {
flexGrow: 1,
Expand Down Expand Up @@ -102,31 +108,29 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({ navigation }) => {
)

const onNotification = useCallback(
async notificationData => {
const {
notification: {
request: {
content: { data },
},
},
} = notificationData
async (res: NotificationResponse) => {
const data = res.notification.request.content.data as NotificationData

const { secretId, uniqueId } = data

const details = secrets.find(({ _id }) => _id === secretId)

if (!details) {
console.error(`Failed to find secret with id ${secretId}`)
return
}

const { secret, issuer, account } = details

showRequestAlert(
issuer,
account,
() => handlePasswordRequest(secret, uniqueId, true),
() => handlePasswordRequest(secret, uniqueId, false)
)
// Delay ensures the prompt is shown, else it's missed some time
setTimeout(() => {
showRequestAlert(
issuer,
account,
() => handlePasswordRequest(secret, uniqueId, true),
() => handlePasswordRequest(secret, uniqueId, false)
)
}, 100)
},
[secrets, handlePasswordRequest]
)
Expand Down

0 comments on commit 026c6f1

Please sign in to comment.