my ionic3项目案例我的 ionic

ionic3接极光推送

2018-05-26  本文已影响35人  南京确善能

需要先上极光创建应用

cordova plugin add jpush-phonegap-plugin --variable APP_KEY=your_jpush_appkey
npm install --save @jiguang-ionic/jpush

Jpush服务

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { JPush } from '@jiguang-ionic/jpush';
import { Platform } from 'ionic-angular';
import { Subject } from 'rxjs';
import {Observable} from "rxjs/Observable";

/**
 * 极光推送服务
 */
@Injectable()
export class JpushProvider {

  constructor(public http: HttpClient, public jpush: JPush,public pla:Platform) {
    jpush.init();
    jpush.setDebugMode(true);
  }

  /**
   * 监听点击推送信息
   */
  openNotification(): Observable<any>{
    return new Observable(observer => {
      document.addEventListener('jpush.openNotification', (event: any) => {
        observer.next(event);  
      })
    });
    
  }

  /**
   * 监听收到推送消息
   */
  receiveNotification(): Observable<any>{
    let content;
    return new Observable(observer => {
      document.addEventListener('jpush.receiveNotification', (event: any) => {
        if (this.pla.is("android")) {
          content = event.alert;
        } else { 
          content = event.aps.alert;
        }  
        this.jpush.setBadge(0);
        this.jpush.setApplicationIconBadgeNumber(0);
      }, false);
      observer.next(content);
    });
  }

 /**
  * 本地推送
  * @param content 添加推送消息
  * @param title 
  * @param extras 自定义的参数
  */
  addLocalNotification(content:string,title:string,extras?: any){
    if (this.pla.is("android")) {
      console.log("extras:"+extras)
      this.jpush.addLocalNotification(0, content, title, 1, 5000,"123");
    } else {
      this.jpush.addLocalNotificationForIOS(5, content, 1, title, extras);
    }
  }
}

app中监听

 /**
   * 监听消息
   */
  getMessage(){
    let request=this.websocket.getMessage().subscribe(result=>{   
     if(""==result){
        return;
     }
     let viewPage=this.appCtrl.getActiveNav().getActive().id;
    // if(viewPage!="ChatPage"){}//推送
    this.jPushProvider.addLocalNotification(result['text'], result['memberName'],result['groupId']);
    });
  }

点击通知栏事件

/**
   * 点击推送消息
   */
  getPushMessage(){
    let request=this.jPushProvider.openNotification().subscribe(result=>{
      if(""==result){
        return;
     }
      this.appCfg.appAlert("dianji",JSON.stringify(result));
    });
  }

官方git:https://github.com/jpush/jpush-phonegap-plugin

上一篇下一篇

猜你喜欢

热点阅读