@janiscommerce/app-push-notification
v0.0.4
Published
This package will take care of performing the main actions for registration to receive notifications in the foreground and background.
Downloads
88
Maintainers
Readme
@janiscommerce/app-push-notification
Library for receiving notifications issued from firebase/janis.
PeerDependencies installation:
In order to receive notifications it is necessary to include the dependency
npm install @react-native-firebase/messaging
Installation:
npm install @janis-commerce/app-push-notification
What is received from firebase?
What is received is an object called RemoteMessage, which contains the data emitted from Firebase and is what triggers the notification listeners. Inside remoteMessage you get the notifications object that contains the information that we could use to render a component with the application in the foreground
For more information about this, read https://rnfirebase.io/reference/messaging/remotemessage
This library provides the following components and methods:
Functions
NotificationProvider(children, appName, events, environment, additionalInfo, channelConfigs) ⇒ null | React.element
It is the main component of the package, it is a HOC that is responsible for handling the logic of subscribing to notifications and receiving messages from the Firebase console. The HOC contains listeners to listen to notifications in the foreground and background, so (unless we cancel the subscription), we will receive notifications from the app even when it is closed.
Kind: global function
Throws:
- null when not receive a children argument
| Param | Type | Description | | --- | --- | --- | | children | React.element | Component that will be rendered within the HOC, and about which the notification will be displayed | | appName | string | name of the aplication | | events | Array.<string> | is an array that will contain the events to which the user wants to subscribe | | environment | string | The environment is necessary for the API that we are going to use to subscribe the device to notifications. | | additionalInfo | object | fields to be sent as part of the body of the subscription request | | channelConfigs | Array.<(string|object)> | is the configuration that will be used to create new notification channels |
Example
import NotificationProvider from '@janiscommerce/app-push-notification'
return (
<NotificationProvider
appName='pickingApp'
events={["picking:session:created","picking:session:assigned"]}
environment='beta'
>
<MyComponent/>
</NotificationProvider>
)
setupBackgroundMessageHandler(callback)
This function is responsible for handling any callbacks from Firebase cloud messaging in the background or with the application closed
Kind: global function
| Param | Type | Description | | --- | --- | --- | | callback | function | is the function that will receive the payload and render it as appropriate |
usePushNotification() ⇒ object
is a hook, which returns the elements contained within the notifications context. Returns an object containing: | name | description | |----------|----------| | deviceToken | Is the token linked to the device, which we use to subscribe it to notifications. | | foregroundNotification | An object containing all data received when a foreground push notification is triggered. | | backgroundNotification | An object containing all data received when a background push notification is triggered. | | subscribeError | An object containing all data received from a notification service subscription failure. | | cancelNotifications | This util is responsible for making the request to unsubscribe from all notification events. If no arguments are received, the request will be made with the previously registered events. | | updateSuscription | This function is responsible for updating the subscription to the notification service | | addNewEvent | This function allows you to add a new event to receive notifications. | | deleteReceivedNotification | An util that clears the foreground or background notification state to the depending on the type it receives by parameter | getSubscribedEvents | This function returns an array with the events to which the user is subscribed. |
Kind: global function
Example
import {usePushNotification} from '@janiscommerce/app-push-notification'
const { deviceToken, foregroundNotification, backgroundNotification} = usePushNotification()