index.html 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>JPush Phonegap Simple Demo</title>
  6. <link href="css/jquery.mobile-1.1.1.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="js/jquery.js"></script>
  8. <script type="text/javascript" src="js/jquery.mobile-1.1.1.js"></script>
  9. <script type="text/javascript" src="cordova.js"></script>
  10. <script type="text/javascript">
  11. var onDeviceReady = function() {
  12. document.addEventListener("jpush.receiveRegistrationId", function (event) {
  13. alert("receiveRegistrationId" + JSON.stringify(event));
  14. $("#registrationId").html(event.registrationId);
  15. }, false)
  16. initiateUI();
  17. };
  18. var getRegistrationID = function() {
  19. window.JPush.getRegistrationID(onGetRegistrationID);
  20. };
  21. var onGetRegistrationID = function(data) {
  22. try {
  23. console.log("JPushPlugin:registrationID is " + data);
  24. if (data.length == 0) {
  25. var t1 = window.setTimeout(getRegistrationID, 1000);
  26. }
  27. $("#registrationId").html(data);
  28. } catch (exception) {
  29. console.log(exception);
  30. }
  31. };
  32. var onTagsWithAlias = function(event) {
  33. try {
  34. console.log("onTagsWithAlias");
  35. var result = "result code:" + event.resultCode + " ";
  36. result += "tags:" + event.tags + " ";
  37. result += "alias:" + event.alias + " ";
  38. $("#tagAliasResult").html(result);
  39. } catch (exception) {
  40. console.log(exception)
  41. }
  42. };
  43. var onOpenNotification = function(event) {
  44. try {
  45. var alertContent;
  46. if (device.platform == "Android") {
  47. alertContent = event.alert;
  48. } else {
  49. alertContent = event.aps.alert;
  50. }
  51. alert("open Notification:" + alertContent);
  52. } catch (exception) {
  53. console.log("JPushPlugin:onOpenNotification" + exception);
  54. }
  55. };
  56. var onReceiveNotification = function(event) {
  57. try {
  58. var alertContent;
  59. if (device.platform == "Android") {
  60. alertContent = event.alert;
  61. } else {
  62. alertContent = event.aps.alert;
  63. }
  64. $("#notificationResult").html(alertContent);
  65. } catch (exception) {
  66. console.log(exception)
  67. }
  68. };
  69. var onReceiveMessage = function(event) {
  70. try {
  71. var message;
  72. if (device.platform == "Android") {
  73. message = event.message;
  74. } else {
  75. message = event.content;
  76. }
  77. $("#messageResult").html(message);
  78. } catch (exception) {
  79. console.log("JPushPlugin:onReceiveMessage-->" + exception);
  80. }
  81. };
  82. var initiateUI = function() {
  83. try {
  84. window.JPush.init();
  85. window.JPush.setDebugMode(true);
  86. window.setTimeout(getRegistrationID, 1000);
  87. if (device.platform != "Android") {
  88. window.JPush.setApplicationIconBadgeNumber(0);
  89. }
  90. } catch (exception) {
  91. console.log(exception);
  92. }
  93. $("#setTags").click(function(ev) {
  94. try {
  95. var tag1 = $("#tagText1").val()
  96. var tag2 = $("#tagText2").val()
  97. var tag3 = $("#tagText3").val()
  98. var tags = []
  99. if (tag1) {
  100. tags.push(tag1)
  101. }
  102. if (tag2) {
  103. tags.push(tag2)
  104. }
  105. if (tag3) {
  106. tags.push(tag3)
  107. }
  108. window.JPush.setTags({ sequence: 1, tags: tags },
  109. function (result) {
  110. $("#tagsResult").html(result.tags)
  111. }, function (error) {
  112. alert(error.code)
  113. })
  114. } catch (exception) {
  115. console.log(exception)
  116. }
  117. })
  118. $("#getAllTags").click(function (event) {
  119. window.JPush.getAllTags({ sequence: 2 },
  120. function (result) {
  121. $("#tagsResult").html(result.tags)
  122. }, function (error) {
  123. alert(error.code)
  124. })
  125. })
  126. $("#cleanTags").click(function (event) {
  127. window.JPush.cleanTags({ sequence: 2 },
  128. function (result) {
  129. alert(result.sequence)
  130. $("#tagsResult").html("")
  131. }, function (error) {
  132. alert(error.code)
  133. })
  134. })
  135. $("#setAlias").click(function (event) {
  136. var alias = $("#aliasText").val()
  137. window.JPush.setAlias({ sequence: 1, alias: alias },
  138. function (result) {
  139. $("#aliasResult").html(result.alias)
  140. }, function (error){
  141. alert(error.code)
  142. })
  143. })
  144. $("#getAlias").click(function (event) {
  145. window.JPush.getAlias({ sequence: 2 },
  146. function (result) {
  147. alert(JSON.stringify(result));
  148. }, function (error) {
  149. alert(error.code)
  150. })
  151. });
  152. $("#deleteAlias").click(function (event) {
  153. window.JPush.deleteAlias({ sequence: 3 },
  154. function (result) {
  155. alert(JSON.stringify(result));
  156. }, function (error) {
  157. alert(error.code)
  158. })
  159. });
  160. };
  161. document.addEventListener("deviceready", onDeviceReady, false);
  162. document.addEventListener("jpush.openNotification", onOpenNotification, false);
  163. document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
  164. document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
  165. </script>
  166. </head>
  167. <body>
  168. <div data-role="page" id="page">
  169. <div data-role="content">
  170. <form>
  171. <div class="ui-body ui-body-b">
  172. <div data-role="fieldcontain">
  173. <center>
  174. <h3>JPushPlugin Example</h3>
  175. </center>
  176. <span name="alias" id="alias"></span>
  177. <hr/>
  178. <label>RegistrationID: </label>
  179. <label id="registrationId">null</label>
  180. </div>
  181. <div data-role="fieldcontain">
  182. <label>Tags: </label>
  183. <table>
  184. <tr>
  185. <td>
  186. <input type="text" id="tagText1" />
  187. </td>
  188. </tr>
  189. <tr>
  190. <td>
  191. <input type="text" id="tagText2" />
  192. </td>
  193. </tr>
  194. <tr>
  195. <td>
  196. <input type="text" id="tagText3" />
  197. </td>
  198. </tr>
  199. </table>
  200. <label>Alias: </label>
  201. <table>
  202. <tr>
  203. <td>
  204. <input type="text" id="aliasText" />
  205. </td>
  206. </tr>
  207. </table>
  208. </div>
  209. <div data-role="fieldcontain">
  210. <input type="button" id="setTags" value="Set tags" />
  211. <input type="button" id="getAllTags" value="Get all tags" />
  212. <input type="button" id="cleanTags" value="Clean tags" />
  213. </div>
  214. <div data-role="fieldcontain">
  215. <input type="button" id="setAlias" value="Set alias" />
  216. <input type="button" id="getAlias" value="Get alias" />
  217. <input type="button" id="deleteAlias" value="Delete alias" />
  218. </div>
  219. <div data-role="fieldcontain">
  220. <label id="tagsPrompt">设置 Tag 的结果:</label>
  221. <label id="tagsResult">null</label>
  222. </div>
  223. <div data-role="fieldcontain">
  224. <label id="aliasPrompt">设置 Alias 的结果:</label>
  225. <label id="aliasResult">null</label>
  226. </div>
  227. <div data-role="fieldcontain">
  228. <label id="notificationPrompt">接受的通知内容:</label>
  229. <label id="notificationResult">null</label>
  230. </div>
  231. <div data-role="fieldcontain">
  232. <label id="messagePrompt">接受的自定义消息:</label>
  233. <label id="messageResult">null</label>
  234. </div>
  235. </div>
  236. </form>
  237. </div>
  238. </div>
  239. </body>
  240. </html>