update get APNS token for ios platform
This commit is contained in:
parent
601bd7fb56
commit
d24c733897
20
App.js
20
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);
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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,7 +84,7 @@ class RegisterProfileScreen extends Component {
|
||||
|
||||
|
||||
async _register() {
|
||||
this.setState({disableButton: true})
|
||||
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) {
|
||||
@ -81,7 +94,7 @@ class RegisterProfileScreen extends Component {
|
||||
|
||||
let systemName = DeviceInfo.getSystemName();
|
||||
let deviceId = null
|
||||
if(systemName == "Android") {
|
||||
if (systemName == "Android") {
|
||||
if (!this.props.device) {
|
||||
Alert.alert(null, 'Cannot get push token or device.', [{ text: 'ok' }])
|
||||
return;
|
||||
@ -108,7 +121,8 @@ class RegisterProfileScreen extends Component {
|
||||
'',
|
||||
t('register_success'),
|
||||
[
|
||||
{ text: 'OK', onPress: () => {
|
||||
{
|
||||
text: 'OK', onPress: () => {
|
||||
/*checkPaymentMeter()
|
||||
.then(result => {
|
||||
if(result.data){
|
||||
@ -125,8 +139,8 @@ class RegisterProfileScreen extends Component {
|
||||
}
|
||||
})*/
|
||||
|
||||
this.setState({isLoading:false},() => {
|
||||
this.props.navigation.navigate('HomeScreen',{isLogin:true})
|
||||
this.setState({ isLoading: false }, () => {
|
||||
this.props.navigation.navigate('HomeScreen', { isLogin: true })
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -141,7 +155,7 @@ class RegisterProfileScreen extends Component {
|
||||
} else {
|
||||
this.props.appSetUser(null)
|
||||
let error_text = res.data.error;
|
||||
if(res.data.errors && res.data.errors.room[0]){
|
||||
if (res.data.errors && res.data.errors.room[0]) {
|
||||
error_text = res.data.errors.room[0]
|
||||
}
|
||||
Alert.alert(null, error_text, [{ text: t('ok') }])
|
||||
@ -163,11 +177,11 @@ class RegisterProfileScreen extends Component {
|
||||
<ScrollView showsVerticalScrollIndicator={false} style={{ flex: 1 }}>
|
||||
<View style={styles.container}>
|
||||
<View style={styles.step_indicator}>
|
||||
<CustomStepIndicator currentStep={3} totalStep={3} style={{ }} />
|
||||
<CustomStepIndicator currentStep={3} totalStep={3} style={{}} />
|
||||
</View>
|
||||
<View style={styles.logo}>
|
||||
<View style={{
|
||||
flexGrow: 1,alignItems: 'center', paddingVertical: 20,
|
||||
flexGrow: 1, alignItems: 'center', paddingVertical: 20,
|
||||
}}>
|
||||
<Image
|
||||
style={{ width: 100, height: 100, borderRadius: 50, borderColor: 'white', borderWidth: 2 }}
|
||||
@ -228,7 +242,7 @@ class RegisterProfileScreen extends Component {
|
||||
<View style={styles.buttons}>
|
||||
<View style={styles.row}>
|
||||
<CustomButton style={styles.btn_next_register} title={'ยืนยัน'} sizeText={14}
|
||||
onPress={this._register} disabled={this.state.disableButton}/>
|
||||
onPress={this._register} disabled={this.state.disableButton} />
|
||||
</View>
|
||||
</View>
|
||||
</LinearGradient>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user