ionic3 +Jpush極光推送
1.先在極光官網註冊賬號,並建立應用,然後獲取應用的APP_KEY。官網網址:https://www.jiguang.cn/push
2.安裝JPush外掛 , github地址:https://github.com/jpush/jpush-phonegap-plugin
有3種安裝方式(3選1),可以在官方網站上檢視:
我用的是第一種安裝方式
cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
APP_KEY是第一步在極光官網建立的應用的祕鑰。
3.安裝 @jiguang-ionic/jpush 包,適配 ionic-native
npm install --save @jiguang-ionic/jpush
然後在 app.module.ts 中新增
import { JPush } from '@jiguang-ionic/jpush';
providers: [
JPush,
]
4.在app.component.ts中初始化
5.在相應的Ts中寫監聽推送的相應事件
//使用者接收到了通知
document.addEventListener(
"jpush.receiveNotification",
(event: any) => {
var content;
if (this.devicePlatform == "Android") {
content = event.alert;
} else {
content = event.aps.alert;
}
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
);
//收到後臺通知
document.addEventListener(
"jpush.backgroundNotification",
(event: any) => {
var onBackgroundNotification = function (event) {
var alertContent = event.aps.alert;
alert("open Notification:" + alertContent);
};
},
false
);
//接收自定義訊息
document.addEventListener("jpush.receiveMessage", (event: any) => {
var onReceiveMessage = function (event) {
try {
var message = event.content
alert("receiveMessage:" + message);
} catch (exception) {
console.log("JPushPlugin:onReceiveMessage-->" + exception);
}
}
}, false);
}
6、打包app,然後在極光網站中傳送訊息
點選下面的立即傳送,這樣app就可以接受到通知了
注意:要看自己新增的android版本是多少,我一開始是7.0.0,然後一直接收不到通知,後面把原來的版本刪掉,然後再重新新增指定的版本(低於7.0.0的版本)就可以了
移出android平臺
ionic cordova platform remove android
新增指定版本的android平臺
ionic cordova platform add [email protected]