@janiscommerce/app-push-notification v0.0.1-beta.1
@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
useNotification() ⇒ 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. |
Kind: global function
Example
import {useNotification} from '@janiscommerce/app-push-notification'
const {} = useNotification()
NotificationProvider(children, foregroundCallback, backgroundCallback, config, events, environment) ⇒ 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 |
foregroundCallback | function | function that will be executed when a foreground notification is received. |
backgroundCallback | function | function that will be executed when a background notification is received. |
config | object | It is an object that contains the user's data, which will be used to subscribe the user to notifications. |
config.appName | string | name of the aplication |
config.accessToken | string | accessToken provided by janis |
config.client | string | client provided by janis |
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. |
Example
import NotificationProvider from '@janiscommerce/app-push-notification'
//...
const foregroundCallback = (remoteMessage) => console.log('a new FCM:',remoteMessage)
const backgrounCallback = (remoteMessage) => {
console.log('a new FCM was received in background', remoteMessage)
}
return (
<NotificationProvider
foregroundCallback={foregroundCallback}
backgroundCallback={backgroundCallback}
config={client:'fizzmod', accessToken:'access_token_push', appName:'janisAppName'}
events={['Notification','events','janis']}
environment='beta'
>
<MyComponent/>
</NotificationProvider>
)
3 months ago