RNpushy热更新
接入pushy
// app.tsx
import { initPushyEvent, removePushyEvent } from '@/utils/pushy';
useEffect(() => {
initPushyEvent();
return () => {
removePushyEvent();
};
}, []);
// utils/pushy.ts
import { Platform, Alert, Linking, AppState } from 'react-native';
import {
isFirstTime,
isRolledBack,
checkUpdate,
downloadUpdate,
switchVersion,
switchVersionLater,
markSuccess,
downloadAndInstallApk
} from 'react-native-update';
import DeviceInfo from 'react-native-device-info';
import { dat } from '@/da';
import AnalyticsUtil from '@/utils/AnalyticsUtil';
import _updateConfig from '../../update.json';
const { appKey } = _updateConfig[Platform.OS];
let stateListener = null;
export const initPushyEvent = () => {
if (isFirstTime) {
// 必须调用此更新成功标记方法
// 否则默认更新失败,下一次启动会自动回滚
markSuccess();
console.log('更新完成');
} else if (isRolledBack) {
console.log('刚刚更新失败了,版本被回滚.');
}
stateListener = AppState.addEventListener('change', (nextAppState) => {
if (nextAppState === 'active') {
checkAppUpdate();
}
});
checkAppUpdate();
};
export const removePushyEvent = () => {
stateListener && stateListener.remove();
};
/**
* phoneBrand数组包含!代表不包含,也就是除了xiaomi、huawei都要更新。如果没有!,则代表只更新xiaomi、huawei
* @param phoneBrand ['xiaomi', 'huawei', '!']
* @param callback 回调函数
*/
const phoneBrandUpdate = (phoneBrand, callback) => {
// 手机品牌
const deviceBrand = DeviceInfo.getBrand().toLowerCase();
if (phoneBrand.includes('!')) {
if (!phoneBrand.includes(deviceBrand)) {
callback();
}
} else {
if (phoneBrand.includes(deviceBrand)) {
callback();
}
}
};
// 更新时机
const updateTimeUpdate = (updateTime, info) => {
switch (updateTime) {
case 'now':
doUpdate(info, 'now');
break;
case 'later':
doUpdate(info, 'later');
break;
default:
doUpdate(info);
}
};
const dealMetaInfo = (info) => {
try {
const metaInfoString = info.metaInfo;
if (metaInfoString) {
// metaJson值
const metaInfoJson = JSON.parse(metaInfoString);
// 有品牌,无时机
if (metaInfoJson.phoneBrand && !metaInfoJson.updateTime) {
phoneBrandUpdate(metaInfoJson.phoneBrand, () => {
doUpdate(info);
});
}
// 有品牌,有时机
if (metaInfoJson.phoneBrand && metaInfoJson.updateTime) {
phoneBrandUpdate(metaInfoJson.phoneBrand, () => {
updateTimeUpdate(metaInfoJson.updateTime, info);
});
}
// 无品牌,有时机
if (!metaInfoJson.phoneBrand && metaInfoJson.updateTime) {
updateTimeUpdate(metaInfoJson.updateTime, info);
}
// 无品牌,无时机
if (!metaInfoJson.phoneBrand && !metaInfoJson.updateTime) {
doUpdate(info);
}
} else {
doUpdate(info);
}
} catch (error) {}
};
const doUpdate = async (info, type?: string) => {
try {
const hash = await downloadUpdate(info, {
onDownloadProgress: ({ received, total }) => {
console.log('onDownloadProgress', received, total);
}
});
if (!hash) {
return;
}
if (type === 'now') {
switchVersion(hash);
} else if (type === 'later') {
switchVersionLater(hash);
} else {
switchVersion(hash);
}
// Alert.alert('提示', '下载完毕,是否重启应用?', [
// {
// text: '是',
// onPress: () => {
// switchVersion(hash);
// }
// },
// { text: '否' },
// {
// text: '下次启动时',
// onPress: () => {
// switchVersionLater(hash);
// }
// }
// ]);
} catch (err) {
console.log('更新失败', err.message);
}
};
export const checkAppUpdate = async () => {
if (__DEV__) {
// 开发模式不支持热更新,跳过检查
return;
}
let info;
try {
info = await checkUpdate(appKey);
} catch (err) {
console.log('更新检查失败', err.message);
return;
}
// info.metaInfo
if (info.expired) {
// Alert.alert('提示', '您的应用版本已更新,点击确定下载安装新版本', [
// {
// text: '确定',
// onPress: () => {
// if (info.downloadUrl) {
// if (
// Platform.OS === 'android' &&
// info.downloadUrl.endsWith('.apk')
// ) {
// downloadAndInstallApk({
// url: info.downloadUrl,
// onDownloadProgress: ({ received, total }) => {
// console.log('onDownloadProgress', received, total);
// }
// });
// } else {
// Linking.openURL(info.downloadUrl);
// }
// }
// }
// }
// ]);
console.log('更新检查失败', info.expired);
} else if (info.upToDate) {
console.log('您的应用版本已是最新');
} else {
// Alert.alert(
// '提示',
// '检查到新的版本' info.name ',是否下载?\n' info.description,
// [
// {
// text: '是',
// onPress: () => {
// doUpdate(info);
// }
// },
// { text: '否' }
// ]
// );
dealMetaInfo(info);
}
};
// package.json
"scripts": {
"pushy:login": "pushy login",
"pushy:select:android": "pushy selectApp --platform android",
"pushy:select:ios": "pushy selectApp --platform ios",
"pushy:source:android": "pushy uploadApk android/app/build/outputs/apk/release/app-armeabi-v7a-release.apk",
"pushy:source:ios": "pushy uploadIpa ios/build/rn-upload-app-temp/rfApp.ipa",
"pushy:update:android": "pushy bundle --platform android",
"pushy:update:ios": "pushy bundle --platform ios"
}
使用
场景:本次开发的版本为1.0.0,现在要进行测试,按照以下顺序先上传到 android 平台。
// 登录pushy账号
yarn pushy:login
// 选择平台(android/ios)
yarn pushy:select:android
yarn pushy:select:ios
// 把打出的包上传到对应的pushy后台
yarn pushy:source:android
yarn pushy:source:ios
执行完之后在 pushy 的后台 可以看到你上传的一个基础包1.0.0,当你修改了一些代码需要进行热更新的时候,执行:
// 热更新到对应的平台
yarn pushy:update:android
yarn pushy:update:ios
执行完后,只要安装了1.0.0版本的app都会静默的进行更新。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfkgjea
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24