ionic3 配置极光推送 ps:使用ionic3-jpush集成,包含推动提示方法

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

注意:    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); //设置标签数组

            }

        })

}

}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值