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

  1. controller('RemoteNotificationCtrl', function ($scope,
  2. $rootScope) {
  3. $scope.message = "on load view success!";
  4. // 当设备就绪时
  5. var onDeviceReady = function () {
  6. $scope.message += "JPushPlugin:Device ready!";
  7. initiateUI();
  8. };
  9. // 设置标签和别名
  10. var onTagsWithAlias = function (event) {
  11. try {
  12. $scope.message += "onTagsWithAlias";
  13. var result = "result code:" + event.resultCode + " ";
  14. result += "tags:" + event.tags + " ";
  15. result += "alias:" + event.alias + " ";
  16. $scope.message += result
  17. $scope.tagAliasResult = result;
  18. } catch (exception) {
  19. console.log(exception)
  20. }
  21. };
  22. // 打开通知的回调函数
  23. var onOpenNotification = function (event) {
  24. try {
  25. var alertContent;
  26. if (device.platform == "Android") {
  27. alertContent = window.plugins.jPushPlugin.openNotification.alert;
  28. } else {
  29. alertContent = event.aps.alert;
  30. }
  31. $scope.message += alertContent;
  32. alert("onOpenNotification:" + alertContent);
  33. } catch (exception) {
  34. console.log("JPushPlugin:onOpenNotification" + exception);
  35. }
  36. };
  37. // 接收到通知时的回调函数
  38. var onReceiveNotification = function (event) {
  39. try {
  40. var alertContent;
  41. if (device.platform == "Android") {
  42. alertContent = window.plugins.jPushPlugin.receiveNotification.alert;
  43. } else {
  44. alertContent = event.aps.alert;
  45. }
  46. $scope.message += alertContent;
  47. $scope.notificationResult = alertContent;
  48. } catch (exception) {
  49. console.log(exception)
  50. }
  51. };
  52. // 接收到消息时的回调函数
  53. var onReceiveMessage = function (event) {
  54. try {
  55. var message;
  56. if (device.platform == "Android") {
  57. message = window.plugins.jPushPlugin.receiveMessage.message;
  58. } else {
  59. message = event.content;
  60. }
  61. $scope.message += message;
  62. $scope.messageResult = message;
  63. } catch (exception) {
  64. console.log("JPushPlugin:onReceiveMessage-->" + exception);
  65. }
  66. };
  67. // 获取RegistrationID
  68. var getRegistrationID = function () {
  69. window.plugins.jPushPlugin.getRegistrationID(function (data) {
  70. try {
  71. console.log("JPushPlugin:registrationID is " + data);
  72. if (data.length == 0) {
  73. var t1 = window.setTimeout(getRegistrationID, 1000);
  74. }
  75. $scope.message += "JPushPlugin:registrationID is " + data;
  76. $scope.registrationID = data;
  77. } catch (exception) {
  78. console.log(exception);
  79. }
  80. });
  81. };
  82. //初始化jpush
  83. var initiateUI = function () {
  84. try {
  85. window.plugins.jPushPlugin.init();
  86. getRegistrationID();
  87. if (device.platform != "Android") {
  88. window.plugins.jPushPlugin.setDebugModeFromIos();
  89. window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
  90. } else {
  91. window.plugins.jPushPlugin.setDebugMode(true);
  92. window.plugins.jPushPlugin.setStatisticsOpen(true);
  93. }
  94. $scope.message += '初始化成功! \r\n';
  95. } catch (exception) {
  96. console.log(exception);
  97. }
  98. }
  99. $scope.formData = {}
  100. // 设置别名和标签
  101. $scope.setTagsAndAlias = function () {
  102. try {
  103. $scope.message += "准备设置tag/alias...";
  104. var tags = [];
  105. if ($scope.formData.tag1 != "") {
  106. tags.push($scope.formData.tag1);
  107. }
  108. if ($scope.formData.tag2 != "") {
  109. tags.push($scope.formData.tag2);
  110. }
  111. window.plugins.jPushPlugin.setTagsWithAlias(tags, $scope.formData.alias);
  112. $scope.message += '设置tags和alias成功! \r\n';
  113. } catch (exception) {
  114. console.log(exception);
  115. }
  116. }
  117. // 添加对回调函数的监听
  118. document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
  119. document.addEventListener("deviceready", onDeviceReady, false);
  120. document.addEventListener("jpush.openNotification", onOpenNotification, false);
  121. document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
  122. document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
  123. })

remoteNotification.html

  1. <ion-view title="Notification">
  2. <ion-content>
  3. <div class="row">
  4. <div class="col">
  5. RegistrationID:{{registrationID}}
  6. </div>
  7. </div>
  8. <div class="row">
  9. <div class="col">
  10. Tags:
  11. </div>
  12. </div>
  13. <div class="row">
  14. <div class="col">
  15. <label class="item item-input">
  16. <input type="text" placeholder="tag1" placeholder="formData.tag1">
  17. </label>
  18. </div>
  19. </div>
  20. <div class="row">
  21. <div class="col">
  22. <label class="item item-input">
  23. <input type="text" placeholder="tag2" placeholder="formData.tag1">
  24. </label>
  25. </div>
  26. </div>
  27. <div class="row">
  28. <div class="col">
  29. Alias:
  30. <label class="item item-input">
  31. <input type="text" placeholder="Alias" ng-model="formData.alias">
  32. </label>
  33. </div>
  34. </div>
  35. <div class="row">
  36. <div class="col">
  37. <button class="button button-positive" ng-click="setTagsAndAlias()">add tags and alias</button>
  38. </div>
  39. </div>
  40. <div class="row">
  41. <div class="col">
  42. 设置tag/alias结果:{{tagAliasResult}} <br> 接受的通知内容:{{notificationResult}} <br> 接受的自定义消息:{{messageResult}}
  43. <br>
  44. </div>
  45. </div>
  46. <div class="row">
  47. <p>{{message}}</p>
  48. </div>
  49. </ion-content>
  50. </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. function的prototype

    prototype只有function才有的属性. var a = function() { this.age = 12; this.name = "haha"; }; a.pro ...

  2. PHP event 事件机制

    PHP event 事件机制   <?php /* * PHP 事件机制 */ class baseClass{ private $_e; public function __set($name ...

  3. Linux Rsync

    一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...

  4. ASP.NET 4.5新特性WebAPI从入门到精通

    在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...

  5. (转)Android: NDK编程入门笔记

    转自: http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html 为何要用到NDK? 概括来说主要分为以下几种情况: 1. 代 ...

  6. HDU5568/BestCoder Round #63 (div.2) B.sequence2 dp+高精度

    sequence2 Problem Description Given an integer array bi with a length of n, please tell me how many ...

  7. 当C++学到第20天的时候我崩溃了(找回刚开始的激情)

    首先声明,我是个使用多语言(ASM/C/C++/Java/Perl)的人,主要使用C++和Java所以我认为我的意见还算中肯.那些否定C++的人,你们是否了解————Borland鼓吹Delphi如何 ...

  8. Facebook揭密:如何让MySQL数据库集群自主运行

    Facebook运行着全球最大的MySQL数据库集群,该集群分布在两个大洲上的多个数据中心中数以千计的服务器上.让人不解的是,Facebook只动用了一个很小的团队来管理这个庞大的MySQL数据库集群 ...

  9. IOS底层数据结构--class

    一.类的数据结构 Class(指针) typedef struct objc_class *Class; /* 这是由编译器为每个类产生的数据结构,这个结构定义了一个类.这个结构是通过编译器在执行时产 ...

  10. PHP-用ThinkPHP和Bootstrap实现用户登录设计

    一.目标 1.用ThinkPHP和Bootstrap实现用户登录设 2.初步界面如下 二.用到的工具及框架 1.ThinkPHP 2.Bootstrap 3.Subline 三.开发环境搭建 1.下载 ...