1.到极光官网注册账号,新建应用获得appkey。

详见:https://www.jiguang.cn/app/list

2.引入jpush插件

详见:https://github.com/jpush/jpush-phonegap-plugin

  • 通过 Cordova Plugins 安装,要求 Cordova CLI 5.0+:
    cordova plugin add jpush-phonegap-plugin --variable API_KEY=xxxxxx

  • 通过 url 安装:
    cordova plugin add https://github.com/jpush/jpush-phonegap-plugin.git --variable API_KEY=xxxxxx

  • 下载到本地安装:
    cordova plugin add [localPath] --variable API_KEY=xxxxxx

注意:安装的时候记得带上极光推送新建应用的appkey

先介绍一下jpush的几个常用的事件:

  • jpush.setTagsWithAlias:设置别名和标签时触发
  • jpush.openNotification:打开推送时触发
  • jpush.receiveNotification:接收到通知时触发
  • jpush.receiveMessage:接收到消息时触发
3:controllers逻辑代码。

controllers.js

controller('RemoteNotificationCtrl', function ($scope,
$rootScope) {
    $scope.message = "on load view success!";
    // 当设备就绪时
    var onDeviceReady = function () {
      $scope.message += "JPushPlugin:Device ready!";
      initiateUI();
    };
    // 设置标签和别名
    var onTagsWithAlias = function (event) {
      try {
        $scope.message += "onTagsWithAlias";
        var result = "result code:" + event.resultCode + " ";
        result += "tags:" + event.tags + " ";
        result += "alias:" + event.alias + " ";
        $scope.message += result
        $scope.tagAliasResult = result;
      } catch (exception) {
        console.log(exception)
      }
    };
    // 打开通知的回调函数
    var onOpenNotification = function (event) {
      try {
        var alertContent;
        if (device.platform == "Android") {
          alertContent = window.plugins.jPushPlugin.openNotification.alert;
        } else {
          alertContent = event.aps.alert;
        }
        $scope.message += alertContent;
        alert("onOpenNotification:" + alertContent);
      } catch (exception) {
        console.log("JPushPlugin:onOpenNotification" + exception);
      }
    };
    // 接收到通知时的回调函数
    var onReceiveNotification = function (event) {
      try {
        var alertContent;
        if (device.platform == "Android") {
          alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
        } else {
          alertContent = event.aps.alert;
        }
        $scope.message += alertContent;
        $scope.notificationResult = alertContent;
      } catch (exception) {
        console.log(exception)
      }
    };

    // 接收到消息时的回调函数
    var onReceiveMessage = function (event) {
      try {
        var message;
        if (device.platform == "Android") {
          message = window.plugins.jPushPlugin.receiveMessage.message;
        } else {
          message = event.content;
        }
        $scope.message += message;
        $scope.messageResult = message;
      } catch (exception) {
        console.log("JPushPlugin:onReceiveMessage-->" + exception);
      }
    };

    // 获取RegistrationID
    var getRegistrationID = function () {
      window.plugins.jPushPlugin.getRegistrationID(function (data) {
        try {
          console.log("JPushPlugin:registrationID is " + data);
          if (data.length == 0) {
            var t1 = window.setTimeout(getRegistrationID, 1000);
          }
          $scope.message += "JPushPlugin:registrationID is " + data;
          $scope.registrationID = data;
        } catch (exception) {
          console.log(exception);
        }
      });

    };
    //初始化jpush
    var initiateUI = function () {
      try {
        window.plugins.jPushPlugin.init();
        getRegistrationID();
        if (device.platform != "Android") {
          window.plugins.jPushPlugin.setDebugModeFromIos();
          window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
        } else {
          window.plugins.jPushPlugin.setDebugMode(true);
          window.plugins.jPushPlugin.setStatisticsOpen(true);
        }
        $scope.message += '初始化成功! \r\n';
      } catch (exception) {
        console.log(exception);
      }
    }

    $scope.formData = {}
    // 设置别名和标签
    $scope.setTagsAndAlias = function () {
      try {
        $scope.message += "准备设置tag/alias...";
        var tags = [];
        if ($scope.formData.tag1 != "") {
          tags.push($scope.formData.tag1);
        }
        if ($scope.formData.tag2 != "") {
          tags.push($scope.formData.tag2);
        }
        window.plugins.jPushPlugin.setTagsWithAlias(tags, $scope.formData.alias);
        $scope.message += '设置tags和alias成功! \r\n';
      } catch (exception) {
        console.log(exception);
      }
    }

    // 添加对回调函数的监听
    document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
    document.addEventListener("deviceready", onDeviceReady, false);
    document.addEventListener("jpush.openNotification", onOpenNotification, false);
    document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
    document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
})

remoteNotification.html

<ion-view title="Notification">
<ion-content>
    <div class="row">
        <div class="col">
            RegistrationID:{{registrationID}}
        </div>
    </div>
    <div class="row">
        <div class="col">
            Tags:
        </div>
    </div>
    <div class="row">
        <div class="col">
            <label class="item item-input">
                <input type="text"  placeholder="tag1" placeholder="formData.tag1">
            </label>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <label class="item item-input">
                <input type="text"  placeholder="tag2" placeholder="formData.tag1">
            </label>
        </div>
    </div>
    <div class="row">
        <div class="col">
            Alias:
            <label class="item item-input">
                <input type="text" placeholder="Alias" ng-model="formData.alias">
            </label>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <button class="button button-positive" ng-click="setTagsAndAlias()">add tags and alias</button>
        </div>
    </div>
    <div class="row">
        <div class="col">
            设置tag/alias结果:{{tagAliasResult}} <br> 接受的通知内容:{{notificationResult}} <br> 接受的自定义消息:{{messageResult}}
            <br>
        </div>
    </div>
    <div class="row">
        <p>{{message}}</p>
    </div>
</ion-content>
</ion-view>

效果

设置别名和标签可实现批量设备推送,比如这里我设置了别名为tonge,那么这条推送消息就只有我这台设备可以收得到,下面是效果图

【1】【2】【3】

技巧分享

通常在开发调试阶段,特别是真机调试时,往往不知道代码运行到哪一行报错,这里有个小技巧,可看到controller和view中我设了个message的变量,来监测代码运行的步骤。有的同学就要说了,用alert岂不是更方便,alert确实可行,但如果在发布上线后你忘记注释掉alert,用户在使用时突然弹出一段json数据,自己脑补用户一脸大写懵逼的表情。不要问我怎么知道的,我是不会告诉你曾经我在做大型web项目开发时,使用ajax请求数据都alert了一遍 ,最后就出现了这样的情景。

ionic中极光推送的集成的更多相关文章

  1. (转载)iOS 极光推送SDK 集成指南

    iOS SDK 集成指南 使用提示 本文匹配的 SDK版本:r1.2.5 以后. 查看最近更新了解最新的SDK更新情况. 产品功能说明 极光推送(JPush)是一个端到端的推送服务,使得服务器端消息能 ...

  2. Ionic JPush极光推送 插件实例

    1.需要去这里注册https://www.jiguang.cn 注册成功获取AppKey 备注填写应用包名规范点,在项目还要用那 2.创建ionic 项目 指定你注册时候的包名(假如:com.ioni ...

  3. 68-Flutter中极光推送的使用

    1.申请极光账号和建立应用 极光推送的官方网址为:https://www.jiguang.cn/ 注册好后,进入'服务中心',然后再进入'开发者平台',点击创建应用. 这时候会出现新页面,让你填写“应 ...

  4. Flutter中极光推送的使用----jpush_flutter

    原文地址:https://www.cnblogs.com/niceyoo/p/11095994.html 1.申请极光账号和建立应用 极光推送的官方网址为:https://www.jiguang.cn ...

  5. iOS 极光推送的集成以及一些集成后的狗血

    1.首先进入极光文档下载激光推送的SDk---传送门http://docs.jiguang.cn/jpush/client/iOS/ios_sdk/   将解压后的lib子文件夹(包含JPUSHSer ...

  6. Ionic JPush极光推送二

    1.看图解决问题   2.解决出现统计代码提示问题 修改这个java 文件 导入命名空间 import cn.jpush.android.api.JPushInterface; 添加方法 @Overr ...

  7. ios极光推送快速集成教程

    内容中包含 base64string 图片造成字符过多,拒绝显示

  8. C#关于HttpClient的应用(二):极光推送IM集成

    public class JPushClient:BaseHttpClient { private String appKey; private String masterSecret; public ...

  9. JPushDemo【极光推送集成,基于v3.1.8版本】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个Demo只是记录极光推送的集成,不能运行. 使用步骤 一.项目组织结构图 注意事项: 1.  导入类文件后需要change包名以 ...

随机推荐

  1. UML状态图(转载)

    概述: 图表本身的名称,阐明该图的目的和其他细节.它描述了在一个系统中的一个组成部分不同的状态.状态是特定的一个系统的组件/对象. 状态图描述了一个状态机.我们阐明的状态机可以被定义为一台机器,它定义 ...

  2. display:inline-block引发的间隙问题解决办法

    在网页布局中我们经常会用到display:inline-block;好处是:能够将块状元素按照内联元素的方式布局,同时能设置宽高.个人感觉很好用,可是用多了慢慢的问题就来了? 1.display:in ...

  3. JS中showModalDialog 详细使用(转)

    基本介绍: showModalDialog()         (IE 4+ 支持) showModelessDialog()      (IE 5+ 支持) window.showModalDial ...

  4. Android的事件处理机制详解(二)-----基于监听的事件处理机制

    基于监听的事件处理机制 前言: 我们开发的app更多的时候是需要与用户的交互----即对用户的操作进行响应 这就涉及到了android的事件处理机制; android给我们提供了两套功能强大的处理机制 ...

  5. Android的Testing和Instrumentation

    Android提供了一系列强大的测试工具,它针对Android的环境,扩展了业内标准的JUnit测试框架.尽管你可以使用JUnit测试Android工程,但Android工具允许你为应用程序的各个方面 ...

  6. PHP使用SOAP调用.net的WebService数据

    需要和一个.net系统进行数据交换,对方提供了一个WebService接口,使用PHP如何调用这个数据呢,下面就看看使用SOAP调用的方法吧 这个与一般的PHP POST或GET传值再查库拿数据的思路 ...

  7. gcc: error trying to exec 'cc1plus': execvp: 没有那个文件或目录

    问题: 解决办法: 1.没安装G++ 可使用 sudo apt-get install g++ 进行安装 2.gcc版本和g++版本不相符 可利用gcc -v和g++ -v 查看版本

  8. Tomcat打印运行时日志(控制台),访问日志,启动日志

    1.sh catlina.sh run以控制台形式输出 2.sever.xml.配置acesslog,设置访问日志输出 Tomcat的访问日志是靠org.apache.catalina.valves. ...

  9. Struts2笔记——文件上传

    文件上传概述 * 要想使用HTML 表单上传一个或多个文件, 必须把 HTML 表单的 enctype 属性设置为multipart/form-data,把它的 method 属性设置为post *  ...

  10. list、set、map的特点

    java 集合(list.set.map)的特点 集合相关的类有一大堆,一般也只用到常用的方法增删改查,而且它它们的方法名也基本一样,所以一直都不知道什么时候用什么集合, 今天趁有空特意从网上整理资料 ...