Complete example

This commit is contained in:
Hevin 2017-12-11 19:06:04 +08:00
parent 509184328d
commit c0c7d904e3
7 changed files with 259 additions and 8 deletions

36
.gitignore vendored
View File

@ -37,13 +37,33 @@ Temporary Items
.apdisk
# Ionic example
./ionic/example/node_modules
./ionic/example/plugins
./ionic/example/config.xml
./ionic/example/ionic.config.json
./ionic/example/package-lock.json
./ionic/example/package.json
./ionic/example/tsconfig.json
./ionic/example/tslint.json
ionic/example/.sourcemaps/*
ionic/example/node_modules/*
ionic/example/plugins/*
ionic/example/config.xml
ionic/example/ionic.config.json
ionic/example/package-lock.json
ionic/example/package.json
ionic/example/tsconfig.json
ionic/example/tslint.json
ionic/example/resources/README\.md
ionic/example/www/*
ionic/example/src/assets/*
ionic/example/src/theme
ionic/example/platforms
ionic/example/src/manifest\.json
ionic/example/resources/android/splash/
ionic/example/resources/
ionic/example/src/service-worker\.js
ionic/example/src/index\.html
ionic/example/src/app/app\.scss
ionic/example/src/app/main\.ts
# End of https://www.gitignore.io/api/macos,apachecordova

View File

@ -0,0 +1,25 @@
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { JPush } from '@jiguang-ionic/jpush';
import { HomePage } from '../pages/home/home';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, jpush: JPush) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
jpush.init();
jpush.setDebugMode(true);
});
}
}

View File

@ -0,0 +1 @@
<ion-nav [root]="rootPage"></ion-nav>

View File

@ -0,0 +1,34 @@
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { Device } from '@ionic-native/device';
import { JPush } from '@jiguang-ionic/jpush';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
@NgModule({
declarations: [
MyApp,
HomePage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
HomePage
],
providers: [
StatusBar,
SplashScreen,
Device,
JPush,
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {}

View File

@ -0,0 +1,36 @@
<ion-header>
<ion-navbar>
<ion-title>
JPush Ionic Example
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item>
<div>Registration Id: {{registrationId}}</div>
<button ion-button full (click)="getRegistrationID()">Get Registration Id</button>
</ion-item>
<ion-item>
<button ion-button full (click)="setTags()">Set tags - Tag1, Tag2</button>
<button ion-button full (click)="addTags()">Add tags - Tag3, Tag4</button>
<button ion-button full (click)="checkTagBindState()">Check tag bind state - Tag1</button>
<button ion-button full (click)="deleteTags()">Delete tags - Tag4</button>
<button ion-button full (click)="getAllTags()">Get all tags</button>
<button ion-button full (click)="cleanTags()">Clean tags</button>
</ion-item>
<ion-item>
<button ion-button full (click)="setAlias()">Set Alias - TestAlias</button>
<button ion-button full (click)="getAlias()">Get Alias</button>
<button ion-button full (click)="deleteAlias()">Delete Alias</button>
</ion-item>
<ion-item>
<button ion-button full (click)="addLocalNotification()">Trigger local notification after 5 seconds</button>
</ion-item>
</ion-list>
</ion-content>

View File

@ -0,0 +1,3 @@
page-home {
}

View File

@ -0,0 +1,132 @@
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { JPush } from '@jiguang-ionic/jpush';
import { Device } from '@ionic-native/device';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
public registrationId: string;
devicePlatform: string;
sequence: number = 0;
tagResultHandler = function(result) {
var sequence: number = result.sequence;
var tags: Array<string> = result.tags == null ? [] : result.tags;
alert('Success!' + '\nSequence: ' + sequence + '\nTags: ' + tags.toString());
};
aliasResultHandler = function(result) {
var sequence: number = result.sequence;
var alias: string = result.alias;
alert('Success!' + '\nSequence: ' + sequence + '\nAlias: ' + alias);
};
errorHandler = function(err) {
var sequence: number = err.sequence;
var code = err.code;
alert('Error!' + '\nSequence: ' + sequence + '\nCode: ' + code);
};
constructor(public navCtrl: NavController, public jpush: JPush, device: Device) {
this.devicePlatform = device.platform;
document.addEventListener('jpush.receiveNotification', (event: any) => {
var content;
if (this.devicePlatform == 'Android') {
content = event.alert;
} else {
content = event.aps.alert;
}
console.log('Receive notification: ' + content);
}, false);
document.addEventListener('jpush.openNotification', (event: any) => {
var content;
if (this.devicePlatform == 'Android') {
content = event.alert;
} else {
content = event.aps.alert;
}
alert('Open notification: ' + content);
}, false);
}
getRegistrationID() {
this.jpush.getRegistrationID()
.then(rId => {
this.registrationId = rId;
});
}
setTags() {
this.jpush.setTags({ sequence: this.sequence++, tags: ['Tag1', 'Tag2']})
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
addTags() {
this.jpush.addTags({ sequence: this.sequence++, tags: ['Tag3', 'Tag4']})
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
checkTagBindState() {
this.jpush.checkTagBindState({ sequence: this.sequence++, tag: 'Tag1' })
.then(result => {
var sequence = result.sequence;
var tag = result.tag;
var isBind = result.isBind;
alert('Sequence: ' + sequence + '\nTag: ' + tag + '\nIsBind: ' + isBind);
}).catch(this.errorHandler);
}
deleteTags() {
this.jpush.deleteTags({ sequence: this.sequence++, tags: ['Tag4']})
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
getAllTags() {
this.jpush.getAllTags({ sequence: this.sequence++ })
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
cleanTags() {
this.jpush.cleanTags({ sequence: this.sequence++ })
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
setAlias() {
this.jpush.setAlias({ sequence: this.sequence++, alias: 'TestAlias' })
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
getAlias() {
this.jpush.getAlias({ sequence: this.sequence++ })
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
deleteAlias() {
this.jpush.deleteAlias({ sequence: this.sequence++ })
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
addLocalNotification() {
if (this.devicePlatform == 'Android') {
this.jpush.addLocalNotification(0, 'Hello JPush', 'JPush', 1, 5000);
} else {
this.jpush.addLocalNotificationForIOS(5, 'Hello JPush', 1, 'noti1');
}
}
}