注意: git地址:https://github.com/jpush/jpush-phonegap-plugin.git
应用的包名一定要和 APP_KEY 对应应用的包名一致,否则极光推送服务无法注册成功。
在使用 8 或以上版本的 Xcode 调试 iOS 项目时,需要先在项目配置界面的 Capabilities 中打开 Push Notifications 开关。
-
通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:
ionic cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
-
或直接通过 url 安装:
ionic cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable APP_KEY=your_jpush_appkey
-
或下载到本地安装:
ionic cordova plugin add Your_Plugin_Path --variable APP_KEY=your_jpush_appkey
参数
-
APP_KEY: 必须设置,JPush 上注册的包名对应的 Appkey
--variable APP_KEY=your_jpush_appkey
-
CHANNEL: 可以不设置,v3.6.0+ 版本开始支持(Android Only),方便开发者统计 APK 分发渠道,默认为 developer-default.
--variable CHANNEL=your_channel
-
同时动态配置 APP_KEY 和 CHANNEL 示例
cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey --variable CHANNEL=your_channel
-
安装模块ionic3-jpush git地址:https://github.com/lh4111/ionic-jpush ps:下载npm install --save @jiguang-ionic/jpush@1.0.2确实监控推送方法,所以弃用。
2、 npm i ionic3-jpush -S
3、在app.module.ts中引入,并加入到@NgModule的 providers 中
import { JPush } from 'ionic3-jpush';
@NgModule({
...
providers: [ JPush ],
})
export class AppModule { }
4、新建ionic g provider push 包含方法供调用(重点调用方法)
public sequence: number = 0;
initPush(callback:any){
this.jpush.openNotification()
.subscribe( res => {
console.log(res);
callback(res);
console.log('收到点击通知事件')
})
this.jpush.receiveNotification()
.subscribe( res => {
console.log(res)
console.log('收到通知')
})
this.jpush.receiveMessage()
.subscribe( res => {
console.log(res)
console.log('收到自定义消息')
})
}
/**
*
* @param alias 设置别名
*/
setAlias(alias:any) {
this.jpush.setAlias({ sequence:this.sequence?this.sequence++:1, alias: alias })
.then(res=>{console.log('设置别名')})
.catch();
}
/**
* 设备的id
*/
getRegistrationID() {
this.jpush.getRegistrationID()
.then(rId => {
this.storage.set("jpushId",rId);
});
}
/**
* 设置标签
* tags:['Tag1', 'Tag2']
*/
setTags(tags:any) {
this.jpush.setTags({ sequence: this.sequence++, tags: tags})
.then(res=>{console.log('设置标签')})
.catch();
}
cleanTags() { //清空极光的标签和别名
this.jpush.cleanTags({ sequence:this.sequence?this.sequence++:1})
.then(res=>{console.log('清除所有标签。')})
.catch();
this.jpush.deleteAlias({ sequence:this.sequence?this.sequence++:1})
.then(res=>{console.log('删除别名')})
.catch();
}
5、在Component中调用方法
import { PushProvider } from '../../providers/push/push';
@Component({
template: `
<ion-nav [root]="rootPage"></ion-nav>`
})
export class MyApp {
constructor (public push: PushProvider){
this.push.jpush.init();
this.push.initPush((res)=>{
this.navCtrl.push(res.extras.page);
});
this.push.getRegistrationID(); //获取极光设备id,存储本地,等待登录使用
this.storage.get("userInfo").then(val=>{
if(val != null){
this.push.setAlias(val.id); // 设置别名 string
this.push.setTags(val.tags); //设置标签数组
}
})
}
}

本文详细介绍了如何在Ionic项目中集成极光推送服务,包括通过Cordova插件安装、配置APP_KEY和CHANNEL参数、使用ionic3-jpush模块、以及在应用中初始化和调用推送相关方法。
845

被折叠的 条评论
为什么被折叠?



