Last active
February 14, 2022 10:22
-
-
Save tolutronics/02fd9bcd43c2d4d053a51e2565df450f to your computer and use it in GitHub Desktop.
Receiving update from code push server, handling the process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SyncStatus } from 'capacitor-codepush/dist/esm/syncStatus'; | |
import { codePush, InstallMode } from 'capacitor-codepush'; | |
import { Component } from '@angular/core'; | |
import { Platform } from '@ionic/angular'; | |
import { SplashScreen } from '@capacitor/splash-screen'; | |
import { App } from '@capacitor/app'; | |
import { | |
IRemotePackage, | |
ILocalPackage, | |
} from 'capacitor-codepush/dist/esm/package'; | |
@Component({ | |
selector: "app-root", | |
templateUrl: "app.component.html", | |
styleUrls: ["app.component.scss"], | |
}) | |
export class AppComponent { | |
constructor(private platform: Platform) { | |
this.initializeApp(); | |
if (platform.is('capacitor')) { | |
this.checkUpdate(); //Checks update on application start. | |
App.addListener('appStateChange', ({ isActive }) => { | |
if (isActive) { | |
this.checkUpdate(); //checks update when app was paused then resumes active state. | |
} | |
}); | |
} else { | |
//nothing | |
} | |
} | |
private initializeApp() { | |
this.platform.ready().then(async() => { | |
SplashScreen.hide(); | |
}) | |
} | |
checkUpdate() { | |
let deploymentKey; | |
if (this.platform.is('ios')) { | |
deploymentKey = environment.codePushIosKey; | |
} else if (this.platform.is('android')) { | |
deploymentKey = environment.codepushAndroidKey; | |
} | |
codePush.checkForUpdate(this.success, this.error, deploymentKey); | |
} | |
async success(remotePackage: IRemotePackage) { | |
if (!remotePackage) { | |
console.log('App is Up to date'); | |
codePush.notifyApplicationReady(); // checks if the last update was successful,(if not successful, it will rollback to previous version) | |
} else { | |
if (!remotePackage.failedInstall) { | |
console.log( | |
'A CodePush update is available. Package hash: ', | |
remotePackage | |
); | |
// DOWNLOAD UPDATE | |
console.log('Downloading =========--=======>'); | |
const result: ILocalPackage = await remotePackage.download(); | |
if (result) { | |
result.install({ | |
installMode: InstallMode.IMMEDIATE, | |
minimumBackgroundDuration: 0, | |
mandatoryInstallMode: InstallMode.IMMEDIATE, | |
}); | |
} | |
console.log('Result of download', result); | |
} else { | |
console.log('The available update was attempted before and failed.'); | |
} | |
} | |
} | |
onPackageDownloaded(localPackage: ILocalPackage) { | |
console.log('Download succeeded.===========>', localPackage.description); | |
localPackage | |
.install({ | |
installMode: InstallMode.IMMEDIATE, | |
minimumBackgroundDuration: 0, | |
mandatoryInstallMode: InstallMode.IMMEDIATE, | |
}) | |
.then(this.onInstallSuccess, this.error); | |
} | |
onInstallSuccess() { | |
console.log('Installation succeeded.'); | |
setTimeout(async () => { | |
codePush.restartApplication(); // restarts the application to patch the update | |
}, 200); | |
} | |
error(error) { | |
console.log('Error===>', error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment