-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add push notification handlers and logic
Signed-off-by: jamshale <[email protected]>
- Loading branch information
Showing
3 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', '') | ||
} | ||
} | ||
}) | ||
} |