-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
110 lines (100 loc) · 3.01 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
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
import React, {Fragment, useEffect} from 'react';
import {
Alert,
AsyncStorage
} from 'react-native';
import {Provider} from 'react-redux'
import 'react-native-gesture-handler'
import {store} from './App/reducers'
import { MenuProvider } from 'react-native-popup-menu';
import firebase from 'react-native-firebase';
import { connect } from 'react-redux';
import AppStarter from './App/navigation'
const App = (props) => {
useEffect(() => {
this.checkPermission();
this.messageListener();
return () => {
this.notificationOpenedListener();
}
}, [])
checkPermission = async() => {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
getToken = async() => {
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log("getting token...........", fcmToken)
if (!fcmToken) {
fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log("token is........", fcmToken)
// user has a device token
await AsyncStorage.setItem('fcmToken', fcmToken);
}else{
console.log("can not get token")
}
}
}
//2
requestPermission = async() => {
console.log("requesting the permission.........")
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
messageListener = async () => {
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.showAlert(title, body);
});
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
this.showAlert('Alert', "Alert")
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
// const notificationOpen = await firebase.notifications().getInitialNotification();
firebase.notifications().getInitialNotification()
.then(payload => {
console.log("payload=======", payload)
});
// if (notificationOpen) {
// const { title, body } = notificationOpen.notification;
// this.showAlert(title, body);
// }
this.messageListener = firebase.messaging().onMessage((message) => {
console.log("I am here",JSON.stringify(message));
});
}
showAlert = (title, body) => {
Alert.alert(
title, body,
[
{ text: 'OK', onPress: () => console.log('OK Pressed') },
],
{ cancelable: false },
);
}
return (
<MenuProvider>
<AppStarter/>
</MenuProvider>
);
};
const mapStateToProps = state => {
return {
token: state.LoginUser.userToken,
review: state.addRoot,
id: state.LoginUser.user_id
};
};
export default connect(mapStateToProps)(App);