|
| 1 | +# cordova-code-push + Ionic example |
| 2 | + |
| 3 | +## Prepare the Ionic APP |
| 4 | + |
| 5 | +`ionic start codepush-ionic-test blank` |
| 6 | + |
| 7 | +`cd codepush-ionic-test` |
| 8 | + |
| 9 | +`ionic cordova plugin add cordova-plugin-code-push` |
| 10 | + |
| 11 | +`sudo npm install --save @ionic-native/code-push` |
| 12 | + |
| 13 | +`ionic cordova platform add ios` |
| 14 | + |
| 15 | +`ionic cordova platform add android` |
| 16 | + |
| 17 | +## Code push set up |
| 18 | + |
| 19 | +`sudo npm install` |
| 20 | + |
| 21 | +`sudo npm install -g code-push-cli` |
| 22 | + |
| 23 | +`code-push login` |
| 24 | + |
| 25 | +`code-push app add codepush-ionic-test-ios ios cordova` |
| 26 | + |
| 27 | +`code-push app add codepush-ionic-test-android android cordova` |
| 28 | + |
| 29 | +**See again the keys:** |
| 30 | + |
| 31 | +`code-push deployment ls codepush-ionic-test-android -k` |
| 32 | + |
| 33 | +`code-push deployment ls codepush-ionic-test-ios -k` |
| 34 | + |
| 35 | +**config.xml**: |
| 36 | +```xml |
| 37 | +<platform name="android"> |
| 38 | + <preference name="CodePushDeploymentKey" value="YOUR-ANDROID-DEPLOYMENT-KEY" /> |
| 39 | +</platform> |
| 40 | +<platform name="ios"> |
| 41 | + <preference name="CodePushDeploymentKey" value="YOUR-IOS-DEPLOYMENT-KEY" /> |
| 42 | +</platform> |
| 43 | +``` |
| 44 | + |
| 45 | +**src/app/app.module.ts** (set `CodePush` as provider): |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { BrowserModule } from '@angular/platform-browser'; |
| 49 | +import { ErrorHandler, NgModule } from '@angular/core'; |
| 50 | +import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular'; |
| 51 | +import { SplashScreen } from '@ionic-native/splash-screen'; |
| 52 | +import { StatusBar } from '@ionic-native/status-bar'; |
| 53 | + |
| 54 | +import { MyApp } from './app.component'; |
| 55 | +import { HomePage } from '../pages/home/home'; |
| 56 | + |
| 57 | +import { CodePush } from '@ionic-native/code-push'; |
| 58 | + |
| 59 | +@NgModule({ |
| 60 | + declarations: [ |
| 61 | + MyApp, |
| 62 | + HomePage |
| 63 | + ], |
| 64 | + imports: [ |
| 65 | + BrowserModule, |
| 66 | + IonicModule.forRoot(MyApp) |
| 67 | + ], |
| 68 | + bootstrap: [IonicApp], |
| 69 | + entryComponents: [ |
| 70 | + MyApp, |
| 71 | + HomePage |
| 72 | + ], |
| 73 | + providers: [ |
| 74 | + StatusBar, |
| 75 | + SplashScreen, |
| 76 | + CodePush, |
| 77 | + {provide: ErrorHandler, useClass: IonicErrorHandler} |
| 78 | + ] |
| 79 | +}) |
| 80 | +export class AppModule {} |
| 81 | + |
| 82 | +``` |
| 83 | + |
| 84 | +**src/app.component.ts** (alert for **updates**): |
| 85 | + |
| 86 | +```typescript |
| 87 | +import { Component } from '@angular/core'; |
| 88 | +import { Platform } from 'ionic-angular'; |
| 89 | +import { StatusBar } from '@ionic-native/status-bar'; |
| 90 | +import { SplashScreen } from '@ionic-native/splash-screen'; |
| 91 | + |
| 92 | +import { HomePage } from '../pages/home/home'; |
| 93 | + |
| 94 | +import { CodePush, InstallMode, SyncStatus } from '@ionic-native/code-push'; |
| 95 | +import { AlertController } from 'ionic-angular/components/alert/alert-controller'; |
| 96 | + |
| 97 | +@Component({ |
| 98 | + templateUrl: 'app.html' |
| 99 | +}) |
| 100 | +export class MyApp { |
| 101 | + rootPage:any = HomePage; |
| 102 | + |
| 103 | + constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, |
| 104 | + private codePush: CodePush, private alertCtrl: AlertController) { |
| 105 | + platform.ready().then(() => { |
| 106 | + // Okay, so the platform is ready and our plugins are available. |
| 107 | + // Here you can do any higher level native things you might need. |
| 108 | + statusBar.styleDefault(); |
| 109 | + splashScreen.hide(); |
| 110 | + this.checkCodePush(); |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + checkCodePush() { |
| 115 | + |
| 116 | + this.codePush.sync({ |
| 117 | + updateDialog: { |
| 118 | + appendReleaseDescription: true, |
| 119 | + descriptionPrefix: "\n\nChange log:\n" |
| 120 | + }, |
| 121 | + installMode: InstallMode.IMMEDIATE |
| 122 | + }).subscribe( |
| 123 | + (data) => { |
| 124 | + console.log('CODE PUSH SUCCESSFUL: ' + data); |
| 125 | + |
| 126 | + }, |
| 127 | + (err) => { |
| 128 | + console.log('CODE PUSH ERROR: ' + err); |
| 129 | + |
| 130 | + } |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Release the update |
| 138 | + |
| 139 | +Modify anything in your app and... |
| 140 | + |
| 141 | +`ionic cordova prepare ios` |
| 142 | + |
| 143 | +`code-push release codepush-ionic-test-ios ./platforms/ios/www/ 0.0.1 --description "Your awesome change description"` |
| 144 | + |
| 145 | +`ionic cordova prepare android` |
| 146 | + |
| 147 | +`code-push release codepush-ionic-test-android ./platforms/android/assets/www/ 0.0.1 --description "Your awesome change description"` |
| 148 | + |
| 149 | +> **Note**: the update will only be released for those devices matching the 0.0.1 version of your app. |
0 commit comments