177 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-07-31 17:08:44 +08:00
import { Component } from "@angular/core";
import { NavController } from "ionic-angular";
import { JPush } from "@jiguang-ionic/jpush";
import { Device } from "@ionic-native/device";
2017-12-11 19:06:04 +08:00
@Component({
2018-07-31 17:08:44 +08:00
selector: "page-home",
templateUrl: "home.html"
2017-12-11 19:06:04 +08:00
})
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;
2018-07-31 17:08:44 +08:00
alert(
"Success!" + "\nSequence: " + sequence + "\nTags: " + tags.toString()
);
2017-12-11 19:06:04 +08:00
};
aliasResultHandler = function(result) {
var sequence: number = result.sequence;
var alias: string = result.alias;
2018-07-31 17:08:44 +08:00
alert("Success!" + "\nSequence: " + sequence + "\nAlias: " + alias);
2017-12-11 19:06:04 +08:00
};
errorHandler = function(err) {
var sequence: number = err.sequence;
var code = err.code;
2018-07-31 17:08:44 +08:00
alert("Error!" + "\nSequence: " + sequence + "\nCode: " + code);
2017-12-11 19:06:04 +08:00
};
2018-07-31 17:08:44 +08:00
constructor(
public navCtrl: NavController,
public jpush: JPush,
device: Device
) {
2017-12-11 19:06:04 +08:00
this.devicePlatform = device.platform;
2018-07-31 17:08:44 +08:00
document.addEventListener(
"jpush.receiveNotification",
(event: any) => {
var content;
if (this.devicePlatform == "Android") {
content = event.alert;
} else {
2017-12-14 17:16:00 +08:00
content = event.aps.alert;
}
2018-07-31 17:08:44 +08:00
alert("Receive notification: " + JSON.stringify(event));
},
false
);
document.addEventListener(
"jpush.openNotification",
(event: any) => {
var content;
if (this.devicePlatform == "Android") {
content = event.alert;
} else {
// iOS
if (event.aps == undefined) {
// 本地通知
content = event.content;
} else {
// APNS
content = event.aps.alert;
}
}
alert("open notification: " + JSON.stringify(event));
},
false
);
document.addEventListener(
"jpush.receiveLocalNotification",
(event: any) => {
// iOS(*,9) Only , iOS(10,*) 将在 jpush.openNotification 和 jpush.receiveNotification 中触发。
var content;
if (this.devicePlatform == "Android") {
} else {
content = event.content;
}
alert("receive local notification: " + JSON.stringify(event));
},
false
);
2017-12-11 19:06:04 +08:00
}
getRegistrationID() {
2018-07-31 17:08:44 +08:00
this.jpush.getRegistrationID().then(rId => {
this.registrationId = rId;
});
2017-12-11 19:06:04 +08:00
}
setTags() {
2018-07-31 17:08:44 +08:00
this.jpush
.setTags({ sequence: this.sequence++, tags: ["Tag1", "Tag2"] })
2017-12-11 19:06:04 +08:00
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
addTags() {
2018-07-31 17:08:44 +08:00
this.jpush
.addTags({ sequence: this.sequence++, tags: ["Tag3", "Tag4"] })
2017-12-11 19:06:04 +08:00
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
checkTagBindState() {
2018-07-31 17:08:44 +08:00
this.jpush
.checkTagBindState({ sequence: this.sequence++, tag: "Tag1" })
2017-12-11 19:06:04 +08:00
.then(result => {
var sequence = result.sequence;
var tag = result.tag;
var isBind = result.isBind;
2018-07-31 17:08:44 +08:00
alert(
"Sequence: " + sequence + "\nTag: " + tag + "\nIsBind: " + isBind
);
})
.catch(this.errorHandler);
2017-12-11 19:06:04 +08:00
}
deleteTags() {
2018-07-31 17:08:44 +08:00
this.jpush
.deleteTags({ sequence: this.sequence++, tags: ["Tag4"] })
2017-12-11 19:06:04 +08:00
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
getAllTags() {
2018-07-31 17:08:44 +08:00
this.jpush
.getAllTags({ sequence: this.sequence++ })
2017-12-11 19:06:04 +08:00
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
cleanTags() {
2018-07-31 17:08:44 +08:00
this.jpush
.cleanTags({ sequence: this.sequence++ })
2017-12-11 19:06:04 +08:00
.then(this.tagResultHandler)
.catch(this.errorHandler);
}
setAlias() {
2018-07-31 17:08:44 +08:00
this.jpush
.setAlias({ sequence: this.sequence++, alias: "TestAlias" })
2017-12-11 19:06:04 +08:00
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
getAlias() {
2018-07-31 17:08:44 +08:00
this.jpush
.getAlias({ sequence: this.sequence++ })
2017-12-11 19:06:04 +08:00
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
deleteAlias() {
2018-07-31 17:08:44 +08:00
this.jpush
.deleteAlias({ sequence: this.sequence++ })
2017-12-11 19:06:04 +08:00
.then(this.aliasResultHandler)
.catch(this.errorHandler);
}
addLocalNotification() {
2018-07-31 17:08:44 +08:00
if (this.devicePlatform == "Android") {
this.jpush.addLocalNotification(0, "Hello JPush", "JPush", 1, 5000);
2017-12-11 19:06:04 +08:00
} else {
2018-07-31 17:08:44 +08:00
this.jpush.addLocalNotificationForIOS(5, "Hello JPush", 1, "localNoti1");
2017-12-11 19:06:04 +08:00
}
}
}