ionic3+cordova 极光推送 记录

本人小白  由于公司项目 需要到推送通知   于是决定选择极光推送    以下是  有的代码是截取的别人的尴尬 记录如下 :

首先去https://www.jiguang.cn/注册一个账号


申请应用  填写你项目应用的包名  在你项目的config.xml里获取到APPKey  记住!

然后安装官方的cordova插件

cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey

再安装本库

cnpm install ionic2-jpush --save

再home.ts里引入

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';

import { Platform } from 'ionic-angular';
import { JPushService } from 'ionic2-jpush/dist'

@Component({
  selector: 'page-home',
  templateUrl: 'home.html',
  providers  : [JPushService]
})

export class HomePage {
    constructor(public navCtrl: NavController, private platform: Platform,private jPushPlugin: JPushService) {
    	let openNotification = this.jPushPlugin.openNotification()
         .subscribe( res => {
           console.log(res);
           console.log('收到点击通知事件')
         })
 
 
       let receiveNotification = this.jPushPlugin.receiveNotification()
         .subscribe( res => {
           console.log(res)
           console.log('收到通知')
         })
 
       let receiveMessage = this.jPushPlugin.receiveMessage()
         .subscribe( res => {
           console.log(res)
           console.log('收到自定义消息')
         })
 
       let backgroundNotification = this.jPushPlugin.backgroundNotification()
         .subscribe( res => {
           console.log(res)
           console.log('收到后台通知')
         })
    }

       /**
    * 注册极光
    */
   init() {
    this.jPushPlugin.init()
    .then(res => alert(res))
    .catch(err => alert(err))
    }
 
    /**
    * 获取ID
    */
    getRegistrationID() {
     this.jPushPlugin.getRegistrationID()
     .then(res => alert(res))
     .catch(err => alert(err))
     }

}


home.html


<ion-header>
  <ion-navbar>
    <ion-title>Home</ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>

  <h2>ionic2-jpush-demo</h2>
  <p>
    ionic2-jpush-demo
  </p>


  <button (click)="init()" ion-button block>Init</button>



  <button (click)="getRegistrationID()" ion-button block>Get RegistrationID</button>



</ion-content>


API  也贴一下吧




如果获取ID失败 或 不匹配 解决如下:

1.首先查看应用包 是否和 极光官网填写一致

2.检查本地项目下的

 platforms/android/AndroidManifest.xml  和 android.json

plugins/android.js 和 fetch.json

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值