From 601bd7fb564caf4c0ed856bfbb3cd89d367adea4 Mon Sep 17 00:00:00 2001 From: kanyanat Date: Mon, 1 Dec 2025 14:40:13 +0700 Subject: [PATCH] fix ios push notification registration --- App.js | 13 +- src/screens/login/Login.js | 315 +++++++++++++++++++------------------ 2 files changed, 166 insertions(+), 162 deletions(-) diff --git a/App.js b/App.js index 012d24a..28ca12b 100644 --- a/App.js +++ b/App.js @@ -31,7 +31,7 @@ TouchableOpacity.defaultProps.activeOpacity = 0.7 // setApiStore(store) class App extends Component { - constructor (props) { + constructor(props) { super(props) this._setDataFromInitState = this._setDataFromInitState.bind(this) } @@ -120,7 +120,7 @@ class App extends Component { } } - _setDataFromInitState () { + _setDataFromInitState() { const appState = store.getState().app let device = appState.device @@ -135,19 +135,22 @@ class App extends Component { if (!device) { this.initNotification() + } else { + this.setPermission() + this.setupNotificationListeners() } console.log('app state', appState, device) } - render () { + render() { return ( - - + + diff --git a/src/screens/login/Login.js b/src/screens/login/Login.js index e93ed33..cd4de02 100644 --- a/src/screens/login/Login.js +++ b/src/screens/login/Login.js @@ -1,17 +1,17 @@ -import React, {Component, useCallback} from 'react'; +import React, { Component, useCallback } from 'react'; import { TouchableOpacity, View, Alert, ScrollView, SafeAreaView, KeyboardAvoidingView, Platform } from 'react-native' import Image from 'react-native-fast-image' import GetWidthHeightDevice from '../../components/GetWidthHeightDevice' import { CustomButton } from '../../components/CustomButton' import { CustomInput } from '../../components/CustomInput' -import {login, payment} from '../../api/UserApi' +import { login, payment } from '../../api/UserApi' import { setToken } from '../../api/api' import { BackgroundImage } from '../../components/BackgroundImage' import LinearGradient from 'react-native-linear-gradient' import Text from '../../components/Text' import { bindActionCreators } from 'redux' -import {appSetDevice, appSetPushToken, appSetToken, appSetUser} from '../../redux/app/action' -import {connect} from 'react-redux' +import { appSetDevice, appSetPushToken, appSetToken, appSetUser } from '../../redux/app/action' +import { connect } from 'react-redux' import IndicatorLoading from '../../components/IndicatorLoading'; import { locale, t } from '../../utils/i18n' import moment from "moment"; @@ -27,8 +27,8 @@ class LoginScreen extends Component { password: null, payment: [], sum_payment: [], - all_payment : [], - room_number_array : [], + all_payment: [], + room_number_array: [], } this._login = this._login.bind(this) } @@ -39,172 +39,173 @@ class LoginScreen extends Component { } - async _login () { + async _login() { this.setState({ isLoading: true }, async () => { if (this.state.username && this.state.password) { - var regex = /^\d+$/; + var regex = /^\d+$/; - let fcmToken = null; - if (regex.test(this.state.username) === true) { - console.log('true') - try { - const authStatus = await messaging().requestPermission({ - alert: true, - sound: true, - badge: true, - }); + let fcmToken = null; + if (regex.test(this.state.username) === true) { + console.log('true') + try { + const authStatus = await messaging().requestPermission({ + alert: true, + sound: true, + badge: true, + }); - console.log('authStatus >>> ', authStatus) - const enabled = - authStatus === messaging.AuthorizationStatus.AUTHORIZED || - authStatus === messaging.AuthorizationStatus.PROVISIONAL; + console.log('authStatus >>> ', authStatus) + const enabled = + authStatus === messaging.AuthorizationStatus.AUTHORIZED || + authStatus === messaging.AuthorizationStatus.PROVISIONAL; - if (enabled) { - console.log('Notification permission granted.'); - fcmToken = await messaging().getToken(); - console.log('fcmToken >>>> ', fcmToken) - } else { - console.log('Notification permission denied.'); - } - } catch (error) { - console.error('Failed to request notification permission:', error); - } - - // const fcmToken = await messaging().getAPNSToken() - console.log('await messaging().getAPNSToken() <<<<<< ', fcmToken) - - - let params = { - // mobile: this.state.username, - username: this.state.username, - password: this.state.password, - fcm_token: fcmToken - } - - console.log('params >> ', params) - // console.log('fcmToken >>>>>>>>>>>>>>>>>>>>>>>>> ', fcmToken) - this.setState({isLoading:true}) - login(params) - .then((res) => { - if (res.ok) { - if (res.data.access_token) { - setToken(res.data.access_token) - this.props.appSetToken(res.data.access_token) - this.props.navigation.navigate('HomeScreen',{isLogin:true}) - let user_data = res.data.user; - this.props.appSetUser(user_data) - } - this.setState({ isLoading: false }) + if (enabled) { + console.log('Notification permission granted.'); + await messaging().registerDeviceForRemoteMessages(); + fcmToken = await messaging().getToken(); + console.log('fcmToken >>>> ', fcmToken) } else { - this.setState({ - isLoading: false - }, () => { - Alert.alert(null, t('credential_invalid')) - }) + console.log('Notification permission denied.'); } - }) + } catch (error) { + console.error('Failed to request notification permission:', error); + } + + // const fcmToken = await messaging().getAPNSToken() + console.log('await messaging().getAPNSToken() <<<<<< ', fcmToken) + + + let params = { + // mobile: this.state.username, + username: this.state.username, + password: this.state.password, + fcm_token: fcmToken + } + + console.log('params >> ', params) + // console.log('fcmToken >>>>>>>>>>>>>>>>>>>>>>>>> ', fcmToken) + this.setState({ isLoading: true }) + login(params) + .then((res) => { + if (res.ok) { + if (res.data.access_token) { + setToken(res.data.access_token) + this.props.appSetToken(res.data.access_token) + this.props.navigation.navigate('HomeScreen', { isLogin: true }) + let user_data = res.data.user; + this.props.appSetUser(user_data) + } + this.setState({ isLoading: false }) + } else { + this.setState({ + isLoading: false + }, () => { + Alert.alert(null, t('credential_invalid')) + }) + } + }) + } else { + Alert.alert(null, t('phone_format_invalid')) + this.setState({ isLoading: false }) + } } else { - Alert.alert(null, t('phone_format_invalid')) + Alert.alert(null, t('required_field')) this.setState({ isLoading: false }) } - } else { - Alert.alert(null, t('required_field')) - this.setState({ isLoading: false }) - } }) }; render() { return ( - - - - - - + + + + + + - - {/* Logo */} - - - - เจริญสินธานี - ({t('room_for_rent')}) + + {/* Logo */} + + + + เจริญสินธานี + ({t('room_for_rent')}) + + - - - - - { - this.setState({ - username: e - }) - }} - onEndEditing={(e) => { - this.setState({ - username: e.nativeEvent.text - }) - }} - inputTextAlign={'left'} - iconName={'ic_user'} - placeholder={t('phone')} - placeholderTextColor={'#FFFFFF40'} - style={styles.form_input} /> - - - + + { + this.setState({ + username: e + }) + }} + onEndEditing={(e) => { + this.setState({ + username: e.nativeEvent.text + }) + }} + inputTextAlign={'left'} + iconName={'ic_user'} + placeholder={t('phone')} + placeholderTextColor={'#FFFFFF40'} + style={styles.form_input} /> + + + { - this.setState({ - password: e - }) - }} - onEndEditing={(e) => { - this.setState({ - password: e.nativeEvent.text - }) - }} - style={styles.form_input} /> - - - this.props.navigation.navigate('ForgetPassword')}> - {t('forget_password')}? - - - - { + this.setState({ + password: e + }) + }} + onEndEditing={(e) => { + this.setState({ + password: e.nativeEvent.text + }) + }} + style={styles.form_input} /> + + + this.props.navigation.navigate('ForgetPassword')}> + {t('forget_password')}? + + + + { this.props.navigation.navigate('HomeScreen') }} - /> - - {/* + sizeText={16} + style={[styles.btn_login]} + onPress={this._login} + //onPress={() => { this.props.navigation.navigate('HomeScreen') }} + /> + + {/* {t('not_a_member')} { this.props.navigation.navigate('Register') }}> @@ -212,13 +213,13 @@ class LoginScreen extends Component { */} - - - - - - - + + + + + + + ) }