极光推送插件

配置

  1. 打开config.xml文件,在插件配置勾选cordova-plugin-device和jpush-phonegap-plugin插件。
  2. 右键config.xml文件,点击打开方式,选择XML Editor,编辑jpush插件的APP_KEY参数。APP_KEY代表的是极光推送官网上需要推送此应用生成的appkey。
  3. 打包生成。
  1. <import id="jpush-phonegap-plugin" type="web" ref="cordova">
  2. <parameter>APP_KEY=value</parameter>
  3. </import>

注意:
如果是自己从github上下载的jpush插件,需要在plugin.xml中将如下代码取消注释:<插件所在目录>/jpush-phonegap-plugin/blob/master/plugin.xml

  1. <hook type="after_platform_add" src="hooks/iosEnablePush.js" />
  2. <hook type="after_plugin_install" src="hooks/iosEnablePush.js" />
  3. <hook type="before_plugin_uninstall" src="hooks/iosDisablePush.js" />

用法

  1. var app = {
  2. // Application Constructor
  3. initialize: function() {
  4. this.bindEvents();
  5. },
  6. bindEvents: function() {
  7. document.addEventListener('deviceready', this.onDeviceReady, false);
  8. },
  9. onDeviceReady: function() {
  10. app.receivedEvent('deviceready');
  11. },
  12. receivedEvent: function(id) {
  13. // 监听加载完成事件
  14. document.addEventListener("deviceready", onDeviceReady, false);
  15. // 设置标签和别名
  16. document.addEventListener("jpush.setTagsWithAlias", onTagsWithAlias, false);
  17. // 打开通知
  18. document.addEventListener("jpush.openNotification", onOpenNotification, false);
  19. // 获取通知内容
  20. document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
  21. // 获取自定义消息推送内容
  22. document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
  23. }
  24. };
  25. // 开始
  26. var onDeviceReady = function() {
  27. alert("JPushPlugin:Device ready!");
  28. initiateUI();
  29. };
  30. var initiateUI = function() {
  31. try {
  32. //初始化
  33. window.plugins.jPushPlugin.init();
  34. getRegistrationID();
  35. if (device.platform != "Android") {
  36. window.plugins.jPushPlugin.setDebugModeFromIos();
  37. window.plugins.jPushPlugin.setApplicationIconBadgeNumber(0);
  38. } else {
  39. window.plugins.jPushPlugin.setDebugMode(true);
  40. window.plugins.jPushPlugin.setStatisticsOpen(true);
  41. }
  42. } catch (exception) {
  43. console.log(exception);
  44. }
  45. $("#setTagWithAliasButton").click(function(ev) {
  46. alert("点击了按钮");
  47. try {
  48. var tag1 = $("#tagText1").val();
  49. var tag2 = $("#tagText2").val();
  50. var tag3 = $("#tagText3").val();
  51. var alias = $("#aliasText").val();
  52. var tags = [];
  53. if (tag1 != "") {
  54. tags.push(tag1);
  55. }
  56. if (tag2 != "") {
  57. tags.push(tag2);
  58. }
  59. if (tag3 != "") {
  60. tags.push(tag3);
  61. }
  62. alert(tags[0]+'...'+tags[1]+"....");
  63. alert(alias);
  64. window.plugins.jPushPlugin.setTagsWithAlias(tags, alias);
  65. } catch (exception) {
  66. alert(exception);
  67. }
  68. })
  69. };
  70. //获取注册ID
  71. var getRegistrationID = function() {
  72. window.plugins.jPushPlugin.getRegistrationID(onGetRegistrationID);
  73. };
  74. // 赋值ID给Dom
  75. var onGetRegistrationID = function(data) {
  76. try {
  77. if (data.length == 0) {
  78. var t1 = window.setTimeout(getRegistrationID, 1000);
  79. }
  80. //将注册ID赋值给DOM上
  81. console.log("即将给最上边的label赋值ID");
  82. $("#registrationId").html(data);
  83. } catch (exception) {
  84. alert(exception);
  85. }
  86. };
  87. // 设置标签和别名
  88. var onTagsWithAlias = function(event) {
  89. try {
  90. var result = "result code:" + event.resultCode + " ";
  91. result += "tags:" + event.tags + " ";
  92. result += "alias:" + event.alias + " ";
  93. alert("即将把tags和alias显示在页面上");
  94. $("#tagAliasResult").html(result);
  95. } catch (exception) {
  96. alert(exception)
  97. }
  98. };
  99. // 打开通知
  100. var onOpenNotification = function(event) {
  101. try {
  102. alert(JSON.stringify(event));
  103. var alertContent;
  104. if (device.platform == "Android") {
  105. alertContent = event.alert;
  106. } else {
  107. alertContent = event.aps.alert;
  108. }
  109. alert("open Notification:" + alertContent);
  110. if (event.extras.count == 3){
  111. alert("即将打开百度");
  112. location.href = "http://www.baidu.com"
  113. }
  114. } catch (exception) {
  115. alert("JPushPlugin:onOpenNotification" + exception);
  116. }
  117. };
  118. //获取通知内容
  119. var onReceiveNotification = function(event) {
  120. try {
  121. var alertContent;
  122. if (device.platform == "Android") {
  123. alertContent = event.alert;
  124. } else {
  125. alertContent = event.aps.alert;
  126. }
  127. alert("把接受的通知显示在页面上");
  128. $("#notificationResult").html(alertContent);
  129. } catch (exception) {
  130. alert(exception)
  131. }
  132. };
  133. // 获取自定义消息推送内容
  134. var onReceiveMessage = function(event) {
  135. try {
  136. var message;
  137. if (device.platform == "Android") {
  138. message = event.message;
  139. } else {
  140. message = event.content;
  141. }
  142. alert("把自定义的消息显示在页面上");
  143. $("#messageResult").html(message);
  144. } catch (exception) {
  145. alert("JPushPlugin:onReceiveMessage-->" + exception);
  146. }
  147. };
  148. summerready = function(){
  149. app.initialize();
  150. }

用例github下载地

文档更新时间: 2018-05-15 15:28