1. 程式人生 > >ionic3+cordova 極光推送 記錄

ionic3+cordova 極光推送 記錄

本人小白  由於公司專案 需要到推送通知   於是決定選擇極光推送    以下是  有的程式碼是擷取的別人的尷尬 記錄如下 :

申請應用  填寫你專案應用的包名  在你專案的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