I'm using code-push to update my react-native app. I don't want to upload it to App Store every time if I make an update, I want to push it directly to the users device.
It works fine, the app is updated but I'm trying to show an notification if the updated is successful.
In the docs they are doing it as follows:
codePush.sync({ updateDialog: true },
(status) => {
switch (status) {
case codePush.SyncStatus.DOWNLOADING_PACKAGE:
// Show "downloading" modal
break;
case codePush.SyncStatus.INSTALLING_UPDATE:
// Hide "downloading" modal
break;
}
},
({ receivedBytes, totalBytes, }) => {
/* Update download modal progress */
}
);
I'm trying to update the state if the update is successful and show the modal, with an button to hide it on click. Thus my code is as follows:
constructor() {
super();
this.state = {
modalVisible: false,
status: ""
}
}
componentDidMount() {
let self = this;
codePush.sync({
updateDialog: true,
installMode: codePush.InstallMode.IMMEDIATE
}, (status) => {
switch (status) {
case codePush.SyncStatus.UPDATE_INSTALLED:
self.setState({modalVisible: true});
break;
}
},
({receivedBytes, totalBytes,}) => {
/* Update download modal progress */
self.setState({status: "Downloaded " + receivedBytes + " of " + totalBytes});
}
);
}
renderUpdateNotificationModal(){
return (
<Modal
animationType={"slide"}
transparent={false}
visible={this.state.modalVisible}
onRequestClose={() => {alert("Modal has been closed.")}}
>
<View style={{marginTop: 22}}>
<View>
<Text>Updated</Text>
<TouchableHighlight onPress={() => {this.setState({modalVisible: false})}}>
<Text>OK</Text>
</TouchableHighlight>
</View>
</View>
</Modal>
)
}
render() {
return (
<View style={styles.container}>
{this.renderUpdateNotificationModal()}
<Text style={styles.welcome}>
Welcome!
</Text>
<Text style={styles.instructions}>
To get started, edit index.ios.js
</Text>
<Text style={styles.instructions}>
Press Cmd+R to reload,{'\n'}
Cmd+D or shake for dev menu
</Text>
<Text style={{fontSize: 18, marginTop: 15, color: "blue", textAlign: "center"}}>{this.state.status}</Text>
</View>
);
}
But the problem is that the modal is shown for a few seconds and then it disappears, without clicking on the button.
As the state is set to true, and then after I few seconds to false in
case codePush.SyncStatus.UPDATE_INSTALLED:
self.setState({modalVisible: true});
break;
Any idea how to solve it?