forked from kacgrzes/expo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from binarapps/feat/expo-notifications
feat(expo-notifications): handle expo-notifications
- Loading branch information
Showing
20 changed files
with
461 additions
and
28 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,58 @@ | ||
# Expo notifications configuration guide | ||
|
||
Expo notifications are already preconfigured in this template. However, you still have to provide some secrets and keys in order to use them across your applications that uses this template. | ||
|
||
<b>Expo Go</b> doesn't require any additional configuration so you can check notifications by copying push token (from `Settings` screen) and test notifications (on RL device) on [expo.dev/notifications](http://expo.dev/notifications) tool. | ||
|
||
## Usage in expo dev client (expo run:\[android:ios\]) | ||
|
||
1. Make sure you have created your account in [expo.dev](http://expo.dev). | ||
2. Sign in to your account using `yarn run login` (or `expo login` inside project directory). | ||
3. Follow platform specific configuration. | ||
|
||
### Android | ||
|
||
1. Configure firebase to get `google-services.json` file - [follow this guide](https://docs.expo.dev/push-notifications/using-fcm/). | ||
2. Make sure that you have changed your `owner` name in `app.json`. | ||
3. Put your `google-services.json` in a project directory and provide path to it in `app.json` in `android` section ex.: | ||
|
||
```json | ||
{ | ||
"expo": { | ||
..., | ||
"owner": "@binarapps", | ||
..., | ||
"android": { | ||
"googleServicesFile": "./path/to/google-services.json" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
4. Provide your `experienceId` in `extra` section in `app.json` typically it follows this scheme - `@owner/slug` ex.: | ||
|
||
```json | ||
{ | ||
"expo": { | ||
..., | ||
"owner": "@binarapps", | ||
"slug": "expo-typescript-template", | ||
..., | ||
"extra": { | ||
"experienceid": "@binarapps/expo-typescript-template" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<b>Make sure that you have provided your own secrets for those fields.</b> | ||
|
||
### iOS | ||
|
||
`iOS` notification credentials are automatically generated (paid apple developer account is required to make them working). | ||
|
||
[You can check this guide how to setup push notifications on iOS.](https://docs.expo.dev/push-notifications/push-notifications-setup/#credentials) | ||
|
||
## Extending `expo-notifications` config | ||
|
||
If u need additional `expo-notifications` config [follow this guide](https://github.com/expo/expo/tree/sdk-47/packages/expo-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
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,15 @@ | ||
import { PermissionStatus } from 'expo-modules-core' | ||
import * as Notifications from 'expo-notifications' | ||
import { Dispatch, SetStateAction } from 'react' | ||
|
||
import createGenericContext from '~utils/createGenericContext' | ||
|
||
export type NotificationContextType = { | ||
permissionStatus?: PermissionStatus | ||
setPermissionStatus: Dispatch<SetStateAction<PermissionStatus | undefined>> | ||
notification?: Notifications.Notification | ||
setNotification: Dispatch<SetStateAction<Notifications.Notification | undefined>> | ||
} | ||
|
||
export const [useNotificationContext, NotificationContextProvider] = | ||
createGenericContext<NotificationContextType>('NotificationContext') |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './AuthContext' | ||
export * from './NotificationContext' | ||
export * from './ColorSchemeContext' |
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './useSignInForm' | ||
export * from './useSignUpForm' | ||
export * from './useSignInForm' |
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,26 @@ | ||
import { useEffect } from 'react' | ||
import { Alert } from 'react-native' | ||
|
||
import { useNotificationContext } from '~contexts' | ||
import { registerForPushNotificationsAsync } from '~services' | ||
|
||
export const useNotificationSetup = () => { | ||
const { notification, setNotification } = useNotificationContext() | ||
|
||
useEffect(() => { | ||
const initNotifications = async () => { | ||
await registerForPushNotificationsAsync() | ||
} | ||
initNotifications() | ||
}, []) | ||
|
||
// CONFIG: Handle in app notification | ||
useEffect(() => { | ||
if (notification) { | ||
Alert.alert('Notification', JSON.stringify(notification), [ | ||
{ text: 'Cancel', onPress: () => setNotification(undefined), style: 'cancel' }, | ||
{ text: 'Ok', onPress: () => setNotification(undefined) }, | ||
]) | ||
} | ||
}, [notification, setNotification]) | ||
} |
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,55 @@ | ||
import * as Notifications from 'expo-notifications' | ||
import { PropsWithChildren, FC, useEffect } from 'react' | ||
|
||
import { NotificationContextProvider, NotificationContextType } from '~contexts' | ||
import { useState, useMemo } from '~hooks' | ||
import { | ||
disableAndroidBackgroundNotificationListener, | ||
getNotificationFromStack, | ||
getNotificationStackLength, | ||
} from '~services' | ||
|
||
export const NotificationProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const [permissionStatus, setPermissionStatus] = | ||
useState<NotificationContextType['permissionStatus']>() | ||
const [notification, setNotification] = useState<NotificationContextType['notification']>() | ||
|
||
useEffect(() => { | ||
const getPermissionStatus = async () => { | ||
const { status } = await Notifications.getPermissionsAsync() | ||
setPermissionStatus(status) | ||
} | ||
getPermissionStatus() | ||
}, []) | ||
|
||
useEffect(() => { | ||
while (getNotificationStackLength() > 0) { | ||
const androidBackgroundNotification = getNotificationFromStack() | ||
if (androidBackgroundNotification) { | ||
setNotification(androidBackgroundNotification) | ||
} | ||
} | ||
disableAndroidBackgroundNotificationListener() | ||
|
||
const notificationResponseReceived = Notifications.addNotificationResponseReceivedListener( | ||
({ notification }) => { | ||
setNotification(notification) | ||
} | ||
) | ||
|
||
const notificationReceived = Notifications.addNotificationReceivedListener((notification) => { | ||
setNotification(notification) | ||
}) | ||
|
||
return () => { | ||
Notifications.removeNotificationSubscription(notificationResponseReceived) | ||
Notifications.removeNotificationSubscription(notificationReceived) | ||
} | ||
}, []) | ||
|
||
const value = useMemo( | ||
() => ({ permissionStatus, setPermissionStatus, notification, setNotification }), | ||
[notification, permissionStatus] | ||
) | ||
return <NotificationContextProvider value={value}>{children}</NotificationContextProvider> | ||
} |
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
Oops, something went wrong.