-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: setup a notifications library * feat: setup a notifications library
- Loading branch information
Showing
21 changed files
with
270 additions
and
8 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
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
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,3 @@ | ||
module.exports = { | ||
openSettings: jest.fn(), | ||
}; |
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,3 @@ | ||
module.exports = { | ||
initialize: jest.fn(), | ||
}; |
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
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
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
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
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 @@ | ||
export { Notifications } from './notifications'; |
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,86 @@ | ||
import { OneSignal } from 'react-native-onesignal'; | ||
|
||
import { config } from '$core/constants'; | ||
|
||
class NotificationsClass { | ||
/* ***** ***** Setup ***** ***** */ | ||
|
||
init() { | ||
OneSignal.initialize(config.oneSignalAppId); | ||
|
||
this.watchForNotificationPress(); | ||
} | ||
|
||
/* ***** ***** User ***** ***** */ | ||
|
||
setUser(userId: string) { | ||
OneSignal.login(userId); | ||
} | ||
|
||
removeUser() { | ||
OneSignal.logout(); | ||
} | ||
|
||
setUserEmail(email: string) { | ||
OneSignal.User.addEmail(email); | ||
} | ||
|
||
removeUserEmail(email: string) { | ||
OneSignal.User.removeEmail(email); | ||
} | ||
|
||
setUserLanguage(language: string) { | ||
OneSignal.User.setLanguage(language); | ||
} | ||
|
||
addTag(key: string, value: string) { | ||
OneSignal.User.addTag(key, value); | ||
} | ||
|
||
removeTag(key: string) { | ||
OneSignal.User.removeTag(key); | ||
} | ||
|
||
/* ***** ***** Status ***** ***** */ | ||
|
||
optOut() { | ||
OneSignal.User.pushSubscription.optOut(); | ||
} | ||
|
||
optIn() { | ||
OneSignal.User.pushSubscription.optIn(); | ||
} | ||
|
||
/* ***** ***** Permission ***** ***** */ | ||
|
||
async checkPermissions() { | ||
const isPermissionGranted = | ||
await OneSignal.Notifications.getPermissionAsync(); | ||
|
||
return isPermissionGranted; | ||
} | ||
|
||
async canRequestPermission() { | ||
const isRequestPossible = | ||
await OneSignal.Notifications.canRequestPermission(); | ||
|
||
return isRequestPossible; | ||
} | ||
|
||
async requestPermissions() { | ||
const isPermissionGranted = | ||
await OneSignal.Notifications.requestPermission(false); | ||
|
||
return isPermissionGranted; | ||
} | ||
|
||
/* ***** ***** Listeners ***** ***** */ | ||
|
||
watchForNotificationPress() { | ||
OneSignal.Notifications.addEventListener('click', (event) => { | ||
console.log('OneSignal: notification clicked:', event); | ||
}); | ||
} | ||
} | ||
|
||
export const Notifications = new NotificationsClass(); |
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,52 @@ | ||
import * as Linking from 'expo-linking'; | ||
import React from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Alert } from 'react-native'; | ||
|
||
import { Logger } from '$core/logger'; | ||
import { Notifications as NotificationsHandler } from '$core/notifications'; | ||
import { Button } from '$shared/uiKit/button'; | ||
import { Box, Text } from '$shared/uiKit/primitives'; | ||
|
||
export const Notifications = () => { | ||
const { t } = useTranslation('miscScreens'); | ||
|
||
const requestNotificationPermission = async () => { | ||
const canRequestPermission = | ||
await NotificationsHandler.canRequestPermission(); | ||
const isPermissionAlreadyGranted = | ||
await NotificationsHandler.checkPermissions(); | ||
|
||
if (!canRequestPermission && !isPermissionAlreadyGranted) { | ||
Linking.openSettings().catch((error: unknown) => { | ||
Logger.error({ | ||
error, | ||
level: 'warning', | ||
message: 'Failed to open settings', | ||
}); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
if (isPermissionAlreadyGranted) { | ||
Alert.alert('Permission already granted'); | ||
|
||
return; | ||
} | ||
|
||
await NotificationsHandler.requestPermissions(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Text variant="large">{t('notifications.title')}</Text> | ||
|
||
<Box alignItems="flex-start" mt="spacing_8"> | ||
<Button.Text onPress={requestNotificationPermission}> | ||
{t('notifications.cta')} | ||
</Button.Text> | ||
</Box> | ||
</> | ||
); | ||
}; |
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 @@ | ||
export { Notifications } from './Notifications'; |
Oops, something went wrong.