Skip to content

Commit 6029a50

Browse files
woodrejsMaciej Szczepanski
and
Maciej Szczepanski
authored
feat: handle foreground push notifications (#161)
* Handle Android push notifications * Add basic docs * Cleanup * Translate docs * Cleanup * Cleanup * Cleanup * Cleanup * Update docs * Cleanup * Revert xcworkspace changes * Apply code review changes Co-authored-by: Maciej Szczepanski <[email protected]>
1 parent b729286 commit 6029a50

File tree

7 files changed

+319
-7
lines changed

7 files changed

+319
-7
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
sidebar_position: 7
3+
---
4+
5+
# 📭 Push notifications example
6+
7+
Here's an example of how you can handle push notifications in your application (when in foreground) using [Firebase](https://rnfirebase.io/)
8+
Let's go then! 💪
9+
10+
## 💡 Setup
11+
12+
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/).
13+
14+
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:
15+
16+
- [@react-native-firebase](https://rnfirebase.io/#prerequisites)
17+
- [@react-native-firebase/messaging](https://rnfirebase.io/messaging/usage#installation)
18+
19+
:::info
20+
Documentation clearly describes the whole process of installation, so I will not focus on it here.
21+
Before proceeding to the next step, make sure you have done all previous steps.
22+
:::
23+
24+
## 👀 Example Implementation
25+
26+
### App in Foreground
27+
28+
To handle push notifications in foreground we have to create a listener which will subscribe for all incoming firebase push events.
29+
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()`:
30+
31+
```jsx
32+
// ** imports
33+
import messaging from '@react-native-firebase/messaging'
34+
import { useNotifications } from 'react-native-notificated'
35+
36+
export const App = () => {
37+
const { notify } = useNotifications()
38+
39+
useEffect(() => {
40+
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
41+
notify('info', {
42+
params: {
43+
title: remoteMessage.notification?.title || 'defalut title',
44+
description: remoteMessage.notification?.body,
45+
},
46+
})
47+
})
48+
49+
return unsubscribe
50+
}, [notify])
51+
52+
return // Some JSX
53+
}
54+
```

example/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,5 @@ buck-out/
5858

5959
# CocoaPods
6060
/ios/Pods/
61+
#firebase
62+
android/app/google-services.json

example/App.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
import 'react-native-gesture-handler'
2-
import React from 'react'
3-
import { NavigationContainer } from '@react-navigation/native'
2+
import React, { useEffect } from 'react'
3+
import messaging from '@react-native-firebase/messaging'
44
import { AppNavigator } from './src/navigation'
5+
import { useNotifications } from 'react-native-notificated'
6+
import { NavigationContainer } from '@react-navigation/native'
57

68
const App = () => {
9+
const { notify } = useNotifications()
10+
11+
useEffect(() => {
12+
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
13+
notify('info', {
14+
params: {
15+
title: remoteMessage.notification?.title || '',
16+
description: remoteMessage.notification?.body,
17+
},
18+
})
19+
})
20+
21+
return unsubscribe
22+
}, [notify])
23+
724
return (
825
<NavigationContainer>
926
<AppNavigator />

example/android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
apply plugin: "com.android.application"
2+
apply plugin: 'com.google.gms.google-services'
23

34
import com.android.build.OutputFile
45

example/android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ buildscript {
1414
}
1515
dependencies {
1616
classpath("com.android.tools.build:gradle:4.2.2")
17+
classpath 'com.google.gms:google-services:4.3.13'
1718
// NOTE: Do not place your application dependencies here; they belong
1819
// in the individual module build.gradle files
1920
}

example/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
1111
},
1212
"dependencies": {
13+
"@react-native-firebase/app": "^15.2.0",
14+
"@react-native-firebase/messaging": "^15.2.0",
1315
"@react-native-masked-view/masked-view": "^0.2.6",
1416
"@react-navigation/drawer": "^6.1.8",
1517
"@react-navigation/native": "^6.0.6",

0 commit comments

Comments
 (0)