import 'react-native-gesture-handler';
import * as React from 'react';
import { Platform, AppState } from 'react-native';
import { Provider } from "react-redux";
import firebase from '@react-native-firebase/app';
import '@react-native-firebase/messaging';
import { NativeBaseProvider } from 'native-base';
import Router from './screens/route.js';
import store from './redux/store';
import DeviceInfo from 'react-native-device-info';
import DB from './components/storage/';
var PushNotification = require("react-native-push-notification");
export default class App extends React.Component {
constructor(props) {
super(props)
}
_isMounted = false
state = {
appState: AppState.currentState,
backgroundCaptureTime: null
}
async componentDidMount() {
this._isMounted = true
this.saveDeviceUUID()
this.notificationAuthorization()
this.createNotificationListener()
AppState.addEventListener('change', this._handleAppStateChange)
}
componentWillUnmount() {
this._isMounted = false
try {
this.messageListener()
this.notificationOpenedListener()
this.notificationListener()
AppState.removeEventListener('change', this._handleAppStateChange)
} catch (error) {}
}
_handleAppStateChange = (nextAppState) => {
if(this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
} else {
}
}
saveDeviceUUID = () => {
DB.set("deviceUUID", DeviceInfo.getUniqueId(), () => {}, (e) => console.log("DEVICE INFO SAVING FAILED!", e))
}
notificationAuthorization = async () => {
const authStatus = await firebase.messaging().requestPermission();
const enabled = authStatus === firebase.messaging.AuthorizationStatus.AUTHORIZED || authStatus === firebase.messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
try {
const token = await firebase.messaging().getToken()
if(token) {
let existingToken = await DB.get("fcmToken") || ""
if(token != existingToken){
DB.set("fcmRegistration", "new", (r) => {}, (e) => {})
DB.set("fcmToken", token, () => console.log("FCM TOKEN SAVED", token), () => console.log("FCM TOKEN SAVING FAILED"))
}
}
console.log('device_token:', token);
} catch (error) {
console.log(error);
}
}
}
createNotificationListener = () => {
this.messageListener = firebase.messaging().onMessage(message => {
const { notification } = message
PushNotification.localNotification({
title: notification.title || "Unioil Loyalty App",
message: notification.body,
playSound: false,
soundName: "default"
})
});
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.messaging().onMessage(async remoteMessage => {
console.log(remoteMessage)
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.messaging().onNotificationOpenedApp(remoteMessage => {
console.log(remoteMessage)
});
firebase.messaging()
.getInitialNotification()
.then(async remoteMessage => {
if (remoteMessage) {
if(Platform.OS == 'ios'){
console.log(
'Notification caused app to open from quit state:',
remoteMessage.data.notification,
);
let result = await DB.AddNotification({
messageId: remoteMessage.data.from,
title: remoteMessage.data.notification.title,
body: remoteMessage.data.notification.body,
visible: true,
delivery: false,
recieved: remoteMessage.data.from
})
console.log("Notifications rendered on background", result)
}else{
console.log(
'Notification caused app to open from quit state:',
remoteMessage.notification,
);
let result = await DB.AddNotification({
messageId: remoteMessage.messageId,
title: remoteMessage.notification.title,
body: remoteMessage.notification.body,
visible: true,
delivery: false,
recieved: remoteMessage.sentTime
})
console.log("Notifications rendered on background", result)
}
}
});
}
render() {
return (
)
}
}
// export default function App(){
// useEffect(() => {
// const uniqueId = DeviceInfo.getUniqueId();
// DB.set("deviceUUID", uniqueId, function(){
// }, function(e){
// console.log("DEVICE INFO SAVING FAILED!", e)
// })
// }, [])
// const GetFCMToken = () => {
// messaging().getToken().then(async fcmToken => {
// if(fcmToken){
// let existingToken = await DB.get("fcmToken") || ""
// if(fcmToken != existingToken){
// DB.set("fcmRegistration", "new", (r) => console.log(r), (e) => console.log(e))
// DB.set("fcmToken", fcmToken, () => console.log("FCM TOKEN SAVED"), () => console.log("FCM TOKEN SAVING FAILED"))
// }
// }
// })
// }
// const NotificationAuthorization = async () => {
// await messaging().registerDeviceForRemoteMessages();
// const enabled = await messaging().hasPermission()
// if (!enabled) {
// try {
// await messaging().requestPermission()
// } catch (error) {
// // permission denied
// return
// }
// }
// GetFCMToken()
// }
// useEffect(() => {
// if(Platform.OS === 'ios'){
// NotificationAuthorization()
// }else{
// GetFCMToken()
// }
// }, [])
// useEffect(() => {
// messaging()
// .getInitialNotification()
// .then(async remoteMessage => {
// if (remoteMessage) {
// if(Platform.OS == 'ios'){
// console.log(
// 'Notification caused app to open from quit state:',
// remoteMessage.data.notification,
// );
// let result = await DB.AddNotification({
// messageId: remoteMessage.data.from,
// title: remoteMessage.data.notification.title,
// body: remoteMessage.data.notification.body,
// visible: true,
// delivery: false,
// recieved: remoteMessage.data.from
// })
// console.log("Notifications rendered on background", result)
// }else{
// console.log(
// 'Notification caused app to open from quit state:',
// remoteMessage.notification,
// );
// let result = await DB.AddNotification({
// messageId: remoteMessage.messageId,
// title: remoteMessage.notification.title,
// body: remoteMessage.notification.body,
// visible: true,
// delivery: false,
// recieved: remoteMessage.sentTime
// })
// console.log("Notifications rendered on background", result)
// }
// }
// });
// }, [])
// return (
//
//
//
//
//
// )
// }