microsoft / react-native-code-push

React Native module for CodePush
http://appcenter.ms
Other
8.98k stars 1.47k forks source link

[Android] CodePush update lost after restarting the App #2619

Closed timothy-pham closed 9 months ago

timothy-pham commented 11 months ago

Hello there. I’m experiencing a strange problem when pushing updates with CodePush on Android Platform. Thank you very much for any help!

When I make a CodePush update the update gets successfully installed on device. But when I restart the app (close and open again), it returns to the original .apk.

This issue is happening on Android.

Steps to Reproduce

  1. Click Button Update -> Install
  2. Update Successfully
  3. Close and open app again

Expected Behavior

After restarting the app, keep the installed codePush version

Actual Behavior

After restarting the app, i get the original .apk what I installed in first time.

Environment

Source Code

App.tsx

import codePush from 'react-native-code-push'

const App = () => {
  return (
    ...
  )
}
const codePushOptions = {
  updateDialog: true,
  installMode: codePush.InstallMode.IMMEDIATE,
  checkFrequency: codePush.CheckFrequency.MANUAL,
}

export default codePush(codePushOptions)(App)

android/settings.gradle

apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
include ':app'
includeBuild('../node_modules/react-native-gradle-plugin')

if (settings.hasProperty("newArchEnabled") && settings.newArchEnabled == "true") {
    include(":ReactAndroid")
    project(":ReactAndroid").projectDir = file('../node_modules/react-native/ReactAndroid')
    include(":ReactAndroid:hermes-engine")
    project(":ReactAndroid:hermes-engine").projectDir = file('../node_modules/react-native/ReactAndroid/hermes-engine')
}
include ':app', ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')

Function used when clicking the update button

  setIsUpdate: (isUpdate: boolean) => void,
  setStatusUpdate: (statusUpdate: string) => void,
  setProgress: (progress: number) => void
) => {
  try {
    setIsUpdate(true)
    setStatusUpdate("Đang kiểm tra phiên bản...")
    const update = await codePush.checkForUpdate()

    if (!update) {
      setIsUpdate(false)
      setOpenPopover(false)
      return ToastAndroid.show("Đây là phiên bản mới nhất.", ToastAndroid.SHORT)
    }

    const codePushStatusDidChange = (status: any) => {
      switch (status) {
        case codePush.SyncStatus.CHECKING_FOR_UPDATE:
          setStatusUpdate("Đang kiểm tra phiên bản...")
          break
        case codePush.SyncStatus.DOWNLOADING_PACKAGE:
          setIsUpdate(true)
          setStatusUpdate("Đang tải xuống phiên bản...")
          break
        case codePush.SyncStatus.INSTALLING_UPDATE:
          setStatusUpdate("Đang cài đặt phiên bản...")
          break
        case codePush.SyncStatus.UP_TO_DATE:
          setStatusUpdate("Đây là phiên bản mới nhất.")
          setIsUpdate(false)
          break
        case codePush.SyncStatus.UPDATE_INSTALLED:
          setStatusUpdate("Cài đặt thành công.")
          setIsUpdate(false)
          break
        case codePush.SyncStatus.UPDATE_IGNORED:
          setStatusUpdate("Đang cập nhật...")
          setIsUpdate(false)
          break
        case codePush.SyncStatus.UNKNOWN_ERROR:
          setStatusUpdate("Đang cập nhật...")
          setIsUpdate(false)
          break
        case codePush.SyncStatus.SYNC_IN_PROGRESS:
          setStatusUpdate("Đang cập nhật...")
          setIsUpdate(false)
          break
        default:
          setIsUpdate(false)
          break
      }
    }

    const codePushDownloadDidProgress = (progress: any) => {
      setProgress(progress.receivedBytes / progress.totalBytes)
    }

    await codePush.sync(
      {
        updateDialog: {
          title: "Update available",
        },
        installMode: codePush.InstallMode.IMMEDIATE,
      },
      codePushStatusDidChange,
      codePushDownloadDidProgress
    )
  } catch (error: any) {
    setIsUpdate(false)
    ToastAndroid.show(error.message, ToastAndroid.SHORT)
  }
}
hotdream1990 commented 11 months ago

@tiendat0811 See my comment may help you Tiến Đạt. https://github.com/microsoft/react-native-code-push/issues/2567#issuecomment-1832047879

kevmmdev commented 9 months ago

I am experiencing the same problem in ios, were you able to solve this @tiendat0811 ?

MikhailSuendukov commented 9 months ago

Hi @tiendat0811 If you are still experiencing this issue, could you please confirm what this issue still persist on latest version (8.2.1)? If no, could you please provide us with logs of the update process and the new launch of your application in order to further investigate the issue.

timothy-pham commented 9 months ago

It has been fixed in the new version 8.2.1, I just updated the package and changed codePush to CodePush. Thanks for all the support!