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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* eslint-disable no-console */
import React, {useEffect} from 'react';
import {Platform, View, Alert} from 'react-native';
import messaging from '@react-native-firebase/messaging';
import AsyncStorage from '@react-native-community/async-storage';
import KEY from '../assets/AsynStorage';
import {showNotificaton, updateNotification} from '../actions/SnackBarAction';
import {connect} from 'react-redux';
import {saveUserToRedux} from '../actions/users';
import {newScreenInit} from '../actions/ScreenInitAction';
import {getDetailUser} from '../apis/Functions/users';
import {convertScreen} from '../Config/Functions';
const FirebaseNotification = (props) => {
const refetchDataUser = async () => {
const res = await getDetailUser({
id: props.user.uid,
platform: Platform.OS,
});
if (res.data.code == 200 && res.data.data) {
props.saveUserToRedux(res.data.data);
}
};
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('Message handled in the background!', remoteMessage);
});
messaging().onNotificationOpenedApp(async (remoteMessage) => {
console.log('On notifi open app-----', remoteMessage);
});
useEffect(() => {
getFcmToken();
}, []);
useEffect(() => {
requestUserPermission();
const unsubscribe = messaging().onMessage(async (remoteMessage) => {
console.log('A new FCM message arrived!', remoteMessage.data);
const {action_type, body, title, record_id} = remoteMessage.data;
if (action_type != 'CUSTOMER_NEWS') refetchDataUser();
props.showNotificaton({
title,
content: body,
screen: convertScreen(action_type),
id_record: record_id,
});
});
// (async () => await messaging().registerDeviceForRemoteMessages())();
messaging().onNotificationOpenedApp((remoteMessage) => {
console.log(
'Notification caused app to open from background state:',
remoteMessage,
);
const {action_type, body, title, record_id} = remoteMessage.data;
props.newScreenInit({
screen: convertScreen(action_type),
id_record: record_id,
});
});
messaging()
.getInitialNotification()
.then((remoteMessage) => {
console.log(remoteMessage);
if (remoteMessage) {
console.log(
'Notification caused app to open from quit state:',
remoteMessage.data,
);
const {action_type, body, title, record_id} = remoteMessage.data;
props.newScreenInit({
screen: convertScreen(action_type),
id_record: record_id,
});
}
});
return unsubscribe;
}, []);
const requestUserPermission = async () => {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
getFcmToken();
}
};
const getFcmToken = async () => {
let fcmToken = await AsyncStorage.getItem(KEY.FIREBASE);
console.log('fcmToken save', fcmToken);
if (!fcmToken) {
fcmToken = await messaging().getToken();
if (fcmToken) {
AsyncStorage.setItem(KEY.FIREBASE, fcmToken);
}
}
};
return <View />;
};
const mapStateToProps = (state) => {
return {
user: state.userReducer,
};
};
export default connect(mapStateToProps, {
showNotificaton,
newScreenInit,
updateNotification,
saveUserToRedux,
})(FirebaseNotification);