Skip to content

Commit

Permalink
Add push notification handlers and logic
Browse files Browse the repository at this point in the history
Signed-off-by: jamshale <[email protected]>
  • Loading branch information
jamshale committed Sep 5, 2023
1 parent 5118a0c commit 3faf92c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/src/components/HomeFooterView.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { useAgent } from '@aries-framework/react-hooks'
import { Button, ButtonType, testIdWithKey, useTheme } from 'aries-bifold'
import React, { useState } from 'react'
import { useTranslation } from 'react-i18next'
import { StyleSheet, View } from 'react-native'
import Icon from 'react-native-vector-icons/MaterialCommunityIcons'

import { surveyMonkeyUrl, surveyMonkeyExitUrl } from '../constants'
import { setDeviceInfo } from '../helpers/PushNotificationsHelper'
import WebDisplay from '../screens/WebDisplay'

import PushNotifications from './PushNotifications'

interface HomeFooterViewProps {
children?: any
}
Expand All @@ -15,6 +19,7 @@ const HomeFooterView: React.FC<HomeFooterViewProps> = ({ children }) => {
const { ColorPallet } = useTheme()
const { t } = useTranslation()
const [surveyVisible, setSurveyVisible] = useState(false)
const { agent } = useAgent()

const styles = StyleSheet.create({
feedbackContainer: {
Expand All @@ -30,6 +35,9 @@ const HomeFooterView: React.FC<HomeFooterViewProps> = ({ children }) => {

const toggleSurveyVisibility = () => setSurveyVisible(!surveyVisible)

// Attempt to set device info for push notifications
setDeviceInfo({ agent })

return (
<View style={styles.feedbackContainer}>
<Button
Expand All @@ -53,6 +61,7 @@ const HomeFooterView: React.FC<HomeFooterViewProps> = ({ children }) => {
onClose={toggleSurveyVisibility}
/>
{children}
<PushNotifications agent={agent} />
</View>
)
}
Expand Down
66 changes: 66 additions & 0 deletions app/src/components/PushNotifications.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Agent } from '@aries-framework/core'
import messaging from '@react-native-firebase/messaging'
import { useEffect } from 'react'
import { request, check, PERMISSIONS, RESULTS } from 'react-native-permissions'

interface Props {
agent: Agent | undefined
}

const PushNotifications = ({ agent }: Props) => {
const backgroundHandler = () => {
return messaging().setBackgroundMessageHandler(async () => {
// Do nothing with background messages. Defaults to login and home screen flow
})
}

const messageHandler = () => {
return messaging().onMessage(async () => {
// Ignore foreground messages
})
}

const requestNotificationPermission = async () => {
if (!agent) return
agent.config.logger.info('Requesting push notification permission...')
const result = await request(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)
agent.config.logger.info(`push notification permission is now [${result}]`)
return result
}

const checkNotificationPermission = async () => {
if (!agent) return
agent.config.logger.info('Checking push notification permission...')
const result = await check(PERMISSIONS.ANDROID.POST_NOTIFICATIONS)
agent.config.logger.info(`push notification permission is [${result}]`)
return result
}

const requestPermission = async () => {
if (!agent) return
const checkPermission = await checkNotificationPermission()
if (checkPermission !== RESULTS.GRANTED) {
const request = await requestNotificationPermission()
if (request !== RESULTS.GRANTED) {
// permission not granted
agent.config.logger.warn(`push notification permission was not granted by user`)
}
}
}

useEffect(() => {
if (!agent) return
const backgroundListener = backgroundHandler()
const unsubscribe = messageHandler()
requestPermission()

return () => {
backgroundListener
unsubscribe
}
}, [agent]) // Reload if agent becomes defined

return null
}

export default PushNotifications
35 changes: 35 additions & 0 deletions app/src/helpers/PushNotificationsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Agent } from '@aries-framework/core'
import AsyncStorage from '@react-native-async-storage/async-storage'
import messaging from '@react-native-firebase/messaging'
import { Config } from 'react-native-config'

interface SetDeviceInfoOptions {
agent: Agent<any> | undefined
}

// Will send the device token to the mediator agent if it is undefined or has changed, otherwise it will do nothing
export async function setDeviceInfo({ agent }: SetDeviceInfoOptions) {
if (!Config.MEDIATOR_USE_PUSH_NOTIFICATIONS) return

const token = await messaging().getToken()
if ((await AsyncStorage.getItem('deviceToken')) === token) return

if (!agent) return

agent.config.logger.info('Change of push notification token detected, sending to agent')
const connections = await agent.connections.findAllByQuery({})
connections.forEach(async (c) => {
if (c.theirLabel === Config.MEDIATOR_LABEL) {
agent.config.logger.info(`Trying to send device info to connection ${c.id}`)
try {
await agent.modules.pushNotificationsFcm.setDeviceInfo(c.id, {
deviceToken: token,
})
AsyncStorage.setItem('deviceToken', token)
} catch (error) {
agent.config.logger.error('Error sending device token info to mediator agent')
AsyncStorage.setItem('deviceToken', '')
}
}
})
}

0 comments on commit 3faf92c

Please sign in to comment.