ionic3接极光推送

需要先上极光创建应用

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));
    });
  }

ios报错


{73431D94-5C3E-2B0E-6662-D0C3ED6A2C51}.jpg
打开 xcode10 -> file -> WorkSpace Settings… -> Build System 改成 Legacy Build System [image]

安卓6.4是收不到推送

将jniLibs里面的文件拷贝到libs中,然后删除jniLibs即可

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容