index.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 badgeNumb = 0;
  44. var onOpenNotification = function(event) {
  45. try {
  46. var alertContent;
  47. if (device.platform == "Android") {
  48. alertContent = event.alert;
  49. } else {
  50. alertContent = event.aps.alert;
  51. }
  52. badgeNumb = badgeNumb - 1;
  53. badgeNumb = badgeNumb<=0 ? 0 : badgeNumb;
  54. window.JPush.setBadgeNumber(badgeNumb);
  55. alert("open Notification:" + alertContent);
  56. } catch (exception) {
  57. console.log("JPushPlugin:onOpenNotification" + exception);
  58. }
  59. };
  60. var onReceiveNotification = function(event) {
  61. try {
  62. var alertContent;
  63. if (device.platform == "Android") {
  64. alertContent = event.alert;
  65. } else {
  66. alertContent = event.aps.alert;
  67. }
  68. $("#notificationResult").html(alertContent);
  69. badgeNumb = badgeNumb + 1;
  70. window.JPush.setBadgeNumber(badgeNumb);
  71. } catch (exception) {
  72. console.log(exception)
  73. }
  74. };
  75. var onReceiveMessage = function(event) {
  76. try {
  77. var message;
  78. if (device.platform == "Android") {
  79. message = event.message;
  80. } else {
  81. message = event.content;
  82. }
  83. $("#messageResult").html(message);
  84. badgeNumb = badgeNumb + 1;
  85. window.JPush.setBadgeNumber(badgeNumb);
  86. } catch (exception) {
  87. console.log("JPushPlugin:onReceiveMessage-->" + exception);
  88. }
  89. };
  90. var onResume = function(event){
  91. try {
  92. badgeNumb = 0
  93. window.JPush.setBadgeNumber(0);
  94. } catch (exception) {
  95. console.log("onResume-->" + exception);
  96. }
  97. }
  98. var initiateUI = function() {
  99. try {
  100. window.JPush.init();
  101. window.JPush.setDebugMode(true);
  102. window.setTimeout(getRegistrationID, 1000);
  103. if (device.platform != "Android") {
  104. window.JPush.setApplicationIconBadgeNumber(0);
  105. }
  106. } catch (exception) {
  107. console.log(exception);
  108. }
  109. $("#setTags").click(function(ev) {
  110. try {
  111. var tag1 = $("#tagText1").val()
  112. var tag2 = $("#tagText2").val()
  113. var tag3 = $("#tagText3").val()
  114. var tags = []
  115. if (tag1) {
  116. tags.push(tag1)
  117. }
  118. if (tag2) {
  119. tags.push(tag2)
  120. }
  121. if (tag3) {
  122. tags.push(tag3)
  123. }
  124. window.JPush.setTags({ sequence: 1, tags: tags },
  125. function (result) {
  126. $("#tagsResult").html(JSON.stringify(result.tags))
  127. }, function (error) {
  128. alert(error.code)
  129. })
  130. } catch (exception) {
  131. console.log(exception)
  132. }
  133. })
  134. $("#getAllTags").click(function (event) {
  135. window.JPush.getAllTags({ sequence: 2 },
  136. function (result) {
  137. $("#tagsResult").html(JSON.stringify(result.tags))
  138. }, function (error) {
  139. alert(error.code)
  140. })
  141. })
  142. $("#cleanTags").click(function (event) {
  143. window.JPush.cleanTags({ sequence: 2 },
  144. function (result) {
  145. alert(result.sequence)
  146. $("#tagsResult").html("")
  147. }, function (error) {
  148. alert(error.code)
  149. })
  150. })
  151. $("#setAlias").click(function (event) {
  152. var alias = $("#aliasText").val()
  153. window.JPush.setAlias({ sequence: 1, alias: alias },
  154. function (result) {
  155. $("#aliasResult").html(result.alias)
  156. }, function (error){
  157. alert(error.code)
  158. })
  159. })
  160. $("#getAlias").click(function (event) {
  161. window.JPush.getAlias({ sequence: 2 },
  162. function (result) {
  163. alert(JSON.stringify(result));
  164. }, function (error) {
  165. alert(error.code)
  166. })
  167. });
  168. $("#deleteAlias").click(function (event) {
  169. window.JPush.deleteAlias({ sequence: 3 },
  170. function (result) {
  171. alert(JSON.stringify(result));
  172. }, function (error) {
  173. alert(error.code)
  174. })
  175. });
  176. };
  177. document.addEventListener("deviceready", onDeviceReady, false);
  178. document.addEventListener("jpush.openNotification", onOpenNotification, false);
  179. document.addEventListener("jpush.receiveNotification", onReceiveNotification, false);
  180. document.addEventListener("jpush.receiveMessage", onReceiveMessage, false);
  181. document.addEventListener("resume", onResume, false);
  182. </script>
  183. </head>
  184. <body>
  185. <div data-role="page" id="page">
  186. <div data-role="content">
  187. <form>
  188. <div class="ui-body ui-body-b">
  189. <div data-role="fieldcontain">
  190. <center>
  191. <h3>JPushPlugin Example</h3>
  192. </center>
  193. <span name="alias" id="alias"></span>
  194. <hr/>
  195. <label>RegistrationID: </label>
  196. <label id="registrationId">null</label>
  197. </div>
  198. <div data-role="fieldcontain">
  199. <label>Tags: </label>
  200. <table>
  201. <tr>
  202. <td>
  203. <input type="text" id="tagText1" />
  204. </td>
  205. </tr>
  206. <tr>
  207. <td>
  208. <input type="text" id="tagText2" />
  209. </td>
  210. </tr>
  211. <tr>
  212. <td>
  213. <input type="text" id="tagText3" />
  214. </td>
  215. </tr>
  216. </table>
  217. <label>Alias: </label>
  218. <table>
  219. <tr>
  220. <td>
  221. <input type="text" id="aliasText" />
  222. </td>
  223. </tr>
  224. </table>
  225. </div>
  226. <div data-role="fieldcontain">
  227. <input type="button" id="setTags" value="Set tags" />
  228. <input type="button" id="getAllTags" value="Get all tags" />
  229. <input type="button" id="cleanTags" value="Clean tags" />
  230. </div>
  231. <div data-role="fieldcontain">
  232. <input type="button" id="setAlias" value="Set alias" />
  233. <input type="button" id="getAlias" value="Get alias" />
  234. <input type="button" id="deleteAlias" value="Delete alias" />
  235. </div>
  236. <div data-role="fieldcontain">
  237. <label id="tagsPrompt">设置 Tag 的结果:</label>
  238. <label id="tagsResult">null</label>
  239. </div>
  240. <div data-role="fieldcontain">
  241. <label id="aliasPrompt">设置 Alias 的结果:</label>
  242. <label id="aliasResult">null</label>
  243. </div>
  244. <div data-role="fieldcontain">
  245. <label id="notificationPrompt">接受的通知内容:</label>
  246. <label id="notificationResult">null</label>
  247. </div>
  248. <div data-role="fieldcontain">
  249. <label id="messagePrompt">接受的自定义消息:</label>
  250. <label id="messageResult">null</label>
  251. </div>
  252. </div>
  253. </form>
  254. </div>
  255. </div>
  256. </body>
  257. </html>