-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
64 lines (57 loc) · 1.87 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import React, {useEffect, useState} from 'react';
import {Text, View, Image} from 'react-native';
import messaging from '@react-native-firebase/messaging';
function App() {
const [notification, setNotification] = useState({
title: undefined,
body: undefined,
image: undefined,
});
const getToken = async () => {
const token = await messaging().getToken();
console.log('.........................: ', token);
};
useEffect(() => {
getToken();
messaging().onMessage(async remoteMessage => {
console.log('A new FCM message arrived!', JSON.stringify(remoteMessage));
setNotification({
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
image: remoteMessage.notification.android.imageUrl,
});
});
messaging().onNotificationOpenedApp(remoteMessage => {
console.log('onNotificationOpenedApp: ', JSON.stringify(remoteMessage));
setNotification({
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
image: remoteMessage.notification.android.imageUrl,
});
});
messaging()
.getInitialNotification()
.then(remoteMessage => {
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
JSON.stringify(remoteMessage),
);
setNotification({
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
image: remoteMessage.notification.android.imageUrl,
});
}
});
}, []);
return (
<View>
<Text>Firebase Messaging</Text>
<Text>{`title: ${notification?.title}`}</Text>
<Text>{`title: ${notification?.body}`}</Text>
<Image source={{uri: notification?.image}} width={500} height={500} />
</View>
);
}
export default App;