diff --git a/App.js b/App.js index 28ca12b..adcf05e 100644 --- a/App.js +++ b/App.js @@ -7,7 +7,7 @@ */ import React, { Component } from 'react' -import { StatusBar, StyleSheet, Text, TouchableOpacity } from 'react-native' +import { StatusBar, StyleSheet, Text, TouchableOpacity, Platform } from 'react-native' import MainNav from './src/navigation/MainNav' import messaging from "@react-native-firebase/messaging" import { persistor, store } from './src/redux/store' @@ -86,6 +86,24 @@ class App extends Component { try { await this.setPermission() + if (Platform.OS === 'ios') { + let apnsToken = await messaging().getAPNSToken(); + let retry = 0; + while (!apnsToken && retry < 10) { + console.log('Waiting for APNs token...'); + await new Promise(resolve => setTimeout(resolve, 1000)); + apnsToken = await messaging().getAPNSToken(); + retry++; + } + console.log('APNs Token:', apnsToken); + + if (!apnsToken) { + console.log('Failed to get APNs token after retries'); + // Decide if we want to return here or try anyway. + // Usually if APNs is missing, getToken will fail. + } + } + // ใช้ getToken() แทน getAPNSToken() const fcmToken = await messaging().getToken(); console.log('FCM Token:', fcmToken); diff --git a/src/screens/login/Login.js b/src/screens/login/Login.js index cd4de02..d175031 100644 --- a/src/screens/login/Login.js +++ b/src/screens/login/Login.js @@ -64,6 +64,19 @@ class LoginScreen extends Component { if (enabled) { console.log('Notification permission granted.'); await messaging().registerDeviceForRemoteMessages(); + + if (Platform.OS === 'ios') { + let apnsToken = await messaging().getAPNSToken(); + let retry = 0; + while (!apnsToken && retry < 10) { + console.log('Waiting for APNs token...'); + await new Promise(resolve => setTimeout(resolve, 1000)); + apnsToken = await messaging().getAPNSToken(); + retry++; + } + console.log('APNs Token:', apnsToken); + } + fcmToken = await messaging().getToken(); console.log('fcmToken >>>> ', fcmToken) } else { diff --git a/src/screens/login/RegisterProfile.js b/src/screens/login/RegisterProfile.js index 1ba8736..fc88b46 100644 --- a/src/screens/login/RegisterProfile.js +++ b/src/screens/login/RegisterProfile.js @@ -1,13 +1,13 @@ import React, { Component } from 'react' -import { ScrollView, TouchableOpacity, View, Alert } from 'react-native' +import { ScrollView, TouchableOpacity, View, Alert, Platform } from 'react-native' import Image from 'react-native-fast-image' import { CustomStepIndicator } from '../../components/StepIndicator' import GetWidthHeightDevice from '../../components/GetWidthHeightDevice' import { CustomButton } from '../../components/CustomButton' import { connect } from 'react-redux' -import {checkPaymentMeter, register, registerDevice, testConnect} from '../../api/UserApi' +import { checkPaymentMeter, register, registerDevice, testConnect } from '../../api/UserApi' import { bindActionCreators } from 'redux' -import {appSetDevice, appSetPushToken, appSetToken, appSetUser, setPopupNotification} from '../../redux/app/action' +import { appSetDevice, appSetPushToken, appSetToken, appSetUser, setPopupNotification } from '../../redux/app/action' import messaging from "@react-native-firebase/messaging"; import moment from 'moment' import { setToken } from '../../api/api' @@ -48,6 +48,19 @@ class RegisterProfileScreen extends Component { initNotification = async () => { await this.setPermission() + + if (Platform.OS === 'ios') { + let apnsToken = await messaging().getAPNSToken(); + let retry = 0; + while (!apnsToken && retry < 10) { + console.log('Waiting for APNs token...'); + await new Promise(resolve => setTimeout(resolve, 1000)); + apnsToken = await messaging().getAPNSToken(); + retry++; + } + console.log('APNs Token:', apnsToken); + } + const fcmToken = await messaging().getToken() console.log('fcmToken', fcmToken) if (fcmToken) { @@ -71,83 +84,84 @@ class RegisterProfileScreen extends Component { async _register() { - this.setState({disableButton: true}) - if (!this.props.device && this.props.push_token) { - const resultSendDevice = await registerDevice(this.props.push_token) - if (resultSendDevice.ok && resultSendDevice.data.success) { - this.props.appSetDevice(resultSendDevice.data.device) + this.setState({ disableButton: true }) + if (!this.props.device && this.props.push_token) { + const resultSendDevice = await registerDevice(this.props.push_token) + if (resultSendDevice.ok && resultSendDevice.data.success) { + this.props.appSetDevice(resultSendDevice.data.device) + } + } + + let systemName = DeviceInfo.getSystemName(); + let deviceId = null + if (systemName == "Android") { + if (!this.props.device) { + Alert.alert(null, 'Cannot get push token or device.', [{ text: 'ok' }]) + return; + } + deviceId = this.props.device.id + } + + register({ + ...this.props.user, + device_id: deviceId, + name: this.props.user.name.replace(/\u00a0/g, /\u0020/), + fcm_token: this.props.push_token + }).then( + (res) => { + if (res.ok && res.data.success) { + let token = res.data.token + //this.props.appSetToken(token) + if (res.data.customer) { + this.props.appSetUser(res.data.customer) + this.props.appSetToken(res.data.token) + setToken(token) + + Alert.alert( + '', + t('register_success'), + [ + { + text: 'OK', onPress: () => { + /*checkPaymentMeter() + .then(result => { + if(result.data){ + let data = { + pending_payment: result.data.pending_payment != null ? result.data.pending_payment : 0, + is_notified_electric: result.data.user.is_notified_electric, + is_notified_overdue: result.data.user.is_notified_overdue, + is_notified_meter: result.data.user.is_notified_meter, + payment_id: result.data.payment_id, + notified_meter_at: result.data.notified_meter_at ? result.data.notified_meter_at : moment().format('DD MMM YYYY') + } + + this.props.setPopupNotification(data) + } + })*/ + + this.setState({ isLoading: false }, () => { + this.props.navigation.navigate('HomeScreen', { isLogin: true }) + }) + } + } + ], + { cancelable: false } + ) + + //Alert.alert(null, t('register_success'), [{ text: t('ok') }]) + //this.props.navigation.navigate('HomeScreen') + + } + } else { + this.props.appSetUser(null) + let error_text = res.data.error; + if (res.data.errors && res.data.errors.room[0]) { + error_text = res.data.errors.room[0] + } + Alert.alert(null, error_text, [{ text: t('ok') }]) + this.props.navigation.goBack() } } - - let systemName = DeviceInfo.getSystemName(); - let deviceId = null - if(systemName == "Android") { - if (!this.props.device) { - Alert.alert(null, 'Cannot get push token or device.', [{ text: 'ok' }]) - return; - } - deviceId = this.props.device.id - } - - register({ - ...this.props.user, - device_id: deviceId, - name: this.props.user.name.replace(/\u00a0/g, /\u0020/), - fcm_token: this.props.push_token - }).then( - (res) => { - if (res.ok && res.data.success) { - let token = res.data.token - //this.props.appSetToken(token) - if (res.data.customer) { - this.props.appSetUser(res.data.customer) - this.props.appSetToken(res.data.token) - setToken(token) - - Alert.alert( - '', - t('register_success'), - [ - { text: 'OK', onPress: () => { - /*checkPaymentMeter() - .then(result => { - if(result.data){ - let data = { - pending_payment: result.data.pending_payment != null ? result.data.pending_payment : 0, - is_notified_electric: result.data.user.is_notified_electric, - is_notified_overdue: result.data.user.is_notified_overdue, - is_notified_meter: result.data.user.is_notified_meter, - payment_id: result.data.payment_id, - notified_meter_at: result.data.notified_meter_at ? result.data.notified_meter_at : moment().format('DD MMM YYYY') - } - - this.props.setPopupNotification(data) - } - })*/ - - this.setState({isLoading:false},() => { - this.props.navigation.navigate('HomeScreen',{isLogin:true}) - }) - } - } - ], - { cancelable: false } - ) - - //Alert.alert(null, t('register_success'), [{ text: t('ok') }]) - //this.props.navigation.navigate('HomeScreen') - - } - } else { - this.props.appSetUser(null) - let error_text = res.data.error; - if(res.data.errors && res.data.errors.room[0]){ - error_text = res.data.errors.room[0] - } - Alert.alert(null, error_text, [{ text: t('ok') }]) - this.props.navigation.goBack() - } - } ); } @@ -163,11 +177,11 @@ class RegisterProfileScreen extends Component { - + {t('project')} - {t('building')} - {t('room2')} + {t('building')} + {t('room2')} { user !== null && @@ -228,7 +242,7 @@ class RegisterProfileScreen extends Component { + onPress={this._register} disabled={this.state.disableButton} />