Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle foreground push notifications #161

Merged
merged 12 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/docs/intro/examples/push-notifications-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
sidebar_position: 7
---

# 📭 Push notifications example

Here's an example of how you can handle push notifications in your application (when in foreground) using [Firebase](https://rnfirebase.io/)
Let's go then! 💪

## 💡 Setup

Before we jump into setup proccess, make sure that you have created a new firebase project. You will need it later to send push notifications. If you haven't done it yet, you can do it [here](https://console.firebase.google.com/).

With a new firebase project created, it's time to proceed with installation of these two packages that will be necessary to handle push notifications:

- [@react-native-firebase](https://rnfirebase.io/#prerequisites)
- [@react-native-firebase/messaging](https://rnfirebase.io/messaging/usage#installation)

:::info
Documentation clearly describes the whole process of installation, so I will not focus on it here.
Before proceeding to the next step, make sure you have done all previous steps.
:::

## 👀 Example Implementation

### App in Foreground

To handle push notifications in foreground we have to create a listener which will subscribe for all incoming firebase push events.
We can do this by using `messaging().onMessage()` from `@react-native-firebase/messaging`. Method `onMessage()` takes an asynchronous function as a parameter, with an argument which is the push notification payload. Now we can use this data to set our in-app notification with a `notify()`:

```jsx
// ** imports
import messaging from '@react-native-firebase/messaging'
import { useNotifications } from 'react-native-notificated'

export const App = () => {
const { notify } = useNotifications()

useEffect(() => {
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
notify('info', {
params: {
title: remoteMessage.notification?.title || 'defalut title',
description: remoteMessage.notification?.body,
},
})
})

return unsubscribe
}, [notify])

return // Some JSX
}
```
2 changes: 2 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ buck-out/

# CocoaPods
/ios/Pods/
#firebase
android/app/google-services.json
21 changes: 19 additions & 2 deletions example/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import 'react-native-gesture-handler'
import React from 'react'
import { NavigationContainer } from '@react-navigation/native'
import React, { useEffect } from 'react'
import messaging from '@react-native-firebase/messaging'
import { AppNavigator } from './src/navigation'
import { useNotifications } from 'react-native-notificated'
import { NavigationContainer } from '@react-navigation/native'

const App = () => {
const { notify } = useNotifications()

useEffect(() => {
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
notify('info', {
params: {
title: remoteMessage.notification?.title || '',
description: remoteMessage.notification?.body,
},
})
})

return unsubscribe
}, [notify])

return (
<NavigationContainer>
<AppNavigator />
Expand Down
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: "com.android.application"
apply plugin: 'com.google.gms.google-services'

import com.android.build.OutputFile

Expand Down
1 change: 1 addition & 0 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ buildscript {
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
classpath 'com.google.gms:google-services:4.3.13'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
2 changes: 2 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
},
"dependencies": {
"@react-native-firebase/app": "^15.2.0",
"@react-native-firebase/messaging": "^15.2.0",
"@react-native-masked-view/masked-view": "^0.2.6",
"@react-navigation/drawer": "^6.1.8",
"@react-navigation/native": "^6.0.6",
Expand Down
Loading