【发布时间】:2017-06-16 09:35:12
【问题描述】:
我正在使用 code-push 来更新我的 react-native 应用程序。我不想每次更新都上传到App Store,我想直接推送到用户设备上。
它工作正常,应用程序已更新,但如果更新成功,我会尝试显示通知。
在docs 中,他们的做法如下:
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 */
}
);
如果更新成功,我正在尝试更新状态并显示模态,并带有一个按钮以在单击时将其隐藏。因此我的代码如下:
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>
);
}
但问题是模式显示了几秒钟然后消失了,没有点击按钮。
由于状态设置为true,然后在我几秒后变为false
case codePush.SyncStatus.UPDATE_INSTALLED:
self.setState({modalVisible: true});
break;
知道怎么解决吗?
【问题讨论】:
-
安装更新后是否有闪屏?我认为您的应用已重新启动
-
@Jiro267 不。模态显示,然后一秒钟后消失,我看到更新的屏幕。屏幕不闪烁。
-
installMode: codePush.InstallMode.IMMEDIATE这应该是根据github.com/Microsoft/react-native-code-push/blob/master/docs/… 强制应用重启。您确定它没有如此快速地重新启动和初始化(上面的应用程序非常小!)以至于您将其误认为是模式关闭和状态重置?似乎指向了这一点。解决方案是codePush.InstallMode.ON_NEXT_RESTART或codePush.InstallMode.ON_NEXT_RESUME
标签: react-native code-push react-native-code-push