access 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"
}
use
Scenario: The version developed this time is 1.0.0, and now to be tested, upload it to the android platform in the following order.
// 登录pushy账号
yarn pushy:login
// 选择平台(android/ios)
yarn pushy:select:android
yarn pushy:select:ios
// 把打出的包上传到对应的pushy后台
yarn pushy:source:android
yarn pushy:source:ios
After the execution, you can see a basic package 1.0.0 uploaded by you in the background of pushy . When you modify some code and need to perform hot update, execute:
// 热更新到对应的平台
yarn pushy:update:android
yarn pushy:update:ios
After the execution, as long as the 1.0.0 version of the app is installed, it will be updated silently.