react应用更新引入react-native-code-push

  1. Install the CodePush CLI 创建codepush账号
    npm install -g code-push-cli
    创建账号:code-push register (弹出注册窗口,注册后会生成一串码)
    (可以用code-push login 登录,然后生成一个文件.注意网络问题会到只不成功,多操作几次)
    注册应用名字 code-push app add MyApp

2.在项目的根目录下执行
npm install –save react-native-code-push

3.在android/settings.gradle 增加
include ‘:app’, ‘:react-native-code-push’
project(‘:react-native-code-push’).projectDir = new File(rootProject.projectDir, ‘../node_modules/react-native-code-push/android/app’)

修改 android/app/build.gradle 增加

dependencies {

compile project(‘:react-native-code-push’)
}

Plugin Configuration (Android)
Update the MainActivity.java file to use CodePush via the following changes:

// 1. Import the plugin class
import com.microsoft.codepush.react.CodePush;

// 2. Optional: extend FragmentActivity if you intend to show a dialog prompting users about updates.
public class MainActivity extends FragmentActivity implements DefaultHardwareBackBtnHandler {

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    // 3. Initialize CodePush with your deployment key and an instance of your MainActivity.
    CodePush codePush = new CodePush("d73bf5d8-4fbd-4e55-a837-accd328a21ba", this);
    ...
    mReactInstanceManager = ReactInstanceManager.builder()
            .setApplication(getApplication())
            ...
            // 4. DELETE THIS LINE --> .setBundleAssetName("index.android.bundle")

            // 5. Let CodePush determine which location to load the most updated bundle from.
            // If there is no updated bundle from CodePush, the location will be the assets
            // folder with the name of the bundle passed in, e.g. index.android.bundle
            .setJSBundleFile(codePush.getBundleUrl("index.android.bundle"))

            // 6. Expose the CodePush module to JavaScript.
            .addPackage(codePush.getReactPackage())
            ...
}

}
….

修改android/app/build.gradle 中的android.defaultConfig.versionName,默认是1.0修改为1.0.0开始
android {

defaultConfig {

versionName “1.0.0”

}

}

插件用法:
最简单的方式就是在应用启动的组件中引入以下方法:
a.引入组件CodePush
var CodePush = require(“react-native-code-push”)

b.在应用启动的方法中调用
CodePush.sync();
提供2中方式:
一种是Silent mode (默认就是这种,会自动下载更新,并在下次重启应用的时候生效)
eg:
CodePush.sync();
另一种是Active mode(当有更新的时候会有提示,可以有一些模式提供选择可以参考接口)
eg:
codePush.sync({ updateDialog: true, installMode: codePush.InstallMode.IMMEDIATE });

codepush提供的接口如下:

checkForUpdate: 检查是否用东西要更新

getCurrentPackage: 获取最新包的一些信息如 ( description, installation time, size).

notifyApplicationReady: 通知 CodePush 你有新应用要发布

restartApp:更新应用并重启应用

sync: 检查更新、下载、并安装也可以定制化一些提示信息

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值