push.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6. <meta name="HandheldFriendly" content="true"/>
  7. <meta name="MobileOptimized" content="320"/>
  8. <title>Hello H5+</title>
  9. <script type="text/javascript" src="../js/common.js"></script>
  10. <script type="text/javascript">
  11. var pushServer = "http://demo.dcloud.net.cn/push/?";
  12. var message = null;
  13. // 监听plusready事件
  14. document.addEventListener( "plusready", function(){
  15. message = document.getElementById("message");
  16. // 监听点击消息事件
  17. plus.push.addEventListener( "click", function( msg ) {
  18. // 判断是从本地创建还是离线推送的消息
  19. switch( msg.payload ) {
  20. case "LocalMSG":
  21. outSet( "点击本地创建消息启动:" );
  22. break;
  23. default:
  24. outSet( "点击离线推送消息启动:");
  25. break;
  26. }
  27. // 提示点击的内容
  28. plus.nativeUI.alert( msg.content );
  29. // 处理其它数据
  30. logoutPushMsg( msg );
  31. }, false );
  32. // 监听在线消息事件
  33. plus.push.addEventListener( "receive", function( msg ) {
  34. if ( msg.aps ) { // Apple APNS message
  35. outSet( "接收到在线APNS消息:" );
  36. } else {
  37. outSet( "接收到在线透传消息:" );
  38. }
  39. logoutPushMsg( msg );
  40. }, false );
  41. }, false );
  42. /**
  43. * 日志输入推送消息内容
  44. */
  45. function logoutPushMsg( msg ) {
  46. outLine( "title: "+msg.title );
  47. outLine( "content: "+msg.content );
  48. if ( msg.payload ) {
  49. if ( typeof(msg.payload)=="string" ) {
  50. outLine( "payload(String): "+msg.payload );
  51. } else {
  52. outLine( "payload(JSON): "+JSON.stringify(msg.payload) );
  53. }
  54. } else {
  55. outLine( "payload: undefined" );
  56. }
  57. if ( msg.aps ) {
  58. outLine( "aps: "+JSON.stringify(msg.aps) );
  59. }
  60. }
  61. /**
  62. * 获取本地推送标识信息
  63. */
  64. function getPushInfo(){
  65. outSet("获取客户端推送标识信息:");
  66. plus.push.getClientInfoAsync(function(info){
  67. outLine('Success');
  68. outLine(JSON.stringify(info));
  69. }, function(e){
  70. outLine('Failed');
  71. outLine(JSON.stringify(e));
  72. });
  73. }
  74. /**
  75. * 本地创建一条推动消息
  76. */
  77. function createLocalPushMsg(){
  78. var options = {cover:false};
  79. var str = dateToStr(new Date());
  80. str += ": 欢迎使用HTML5+创建本地消息!";
  81. plus.push.createMessage( str, "LocalMSG", options );
  82. outSet( "创建本地消息成功!" );
  83. outLine( "请到系统消息中心查看!" );
  84. if(plus.os.name=="iOS"){
  85. outLine('*如果无法创建消息,请到"设置"->"通知"中配置应用在通知中心显示!');
  86. }
  87. }
  88. /**
  89. * 读取所有推送消息
  90. */
  91. function listAllPush(){
  92. var msgs=null;
  93. switch ( plus.os.name ) {
  94. case "Android":
  95. msgs = plus.push.getAllMessage();
  96. break;
  97. default:
  98. break;
  99. }
  100. if ( !msgs ) {
  101. outSet( "此平台不支持枚举推送消息列表!" );
  102. return;
  103. }
  104. outSet( "枚举消息列表("+msgs.length+"):" );
  105. for ( var i in msgs ) {
  106. var msg = msgs[i];
  107. outLine( i+": "+msg.title+" - "+msg.content );
  108. }
  109. }
  110. /**
  111. * 清空所有推动消息
  112. */
  113. function clearAllPush(){
  114. plus.push.clear();
  115. outSet( "清空所有推送消息成功!" );
  116. }
  117. /**
  118. * 请求‘简单通知’推送消息
  119. */
  120. function requireNotiMsg(){
  121. if(navigator.userAgent.indexOf('StreamApp')>0){
  122. plus.nativeUI.toast('当前环境暂不支持发送推送消息');
  123. return;
  124. }
  125. var inf = plus.push.getClientInfo();
  126. var url = pushServer+'type=noti&appid='+encodeURIComponent(plus.runtime.appid);
  127. inf.id&&(url+='&id='+inf.id);
  128. url += ('&cid='+encodeURIComponent(inf.clientid));
  129. if(plus.os.name == 'iOS'){
  130. url += ('&token='+encodeURIComponent(inf.token));
  131. }
  132. url += ('&title='+encodeURIComponent('Hello H5+'));
  133. url += ('&content='+encodeURIComponent('欢迎回来体验HTML5 plus应用!'));
  134. url += ('&version='+encodeURIComponent(plus.runtime.version));
  135. plus.runtime.openURL( url );
  136. }
  137. /**
  138. * 请求‘打开网页’推送消息
  139. */
  140. function requireLinkMsg(){
  141. if(navigator.userAgent.indexOf('StreamApp')>0){
  142. plus.nativeUI.toast('当前环境暂不支持发送推送消息');
  143. return;
  144. }
  145. var inf = plus.push.getClientInfo();
  146. var url = pushServer+"type=link&appid="+encodeURIComponent(plus.runtime.appid);
  147. inf.id&&(url+='&id='+inf.id);
  148. url += ('&cid='+encodeURIComponent(inf.clientid));
  149. if(plus.os.name == 'iOS'){
  150. url += ('&token='+encodeURIComponent(inf.token));
  151. }
  152. url += ('&title='+encodeURIComponent('HBuilder飞一样的编码'));
  153. url += ('&content='+encodeURIComponent('看HBuilder如何追求代码编写速度的极致!立即去瞧一瞧?'));
  154. url += ('&url='+encodeURIComponent('http://www.dcloud.io/'));
  155. url += ('&version='+encodeURIComponent(plus.runtime.version));
  156. plus.runtime.openURL( url );
  157. }
  158. /**
  159. * 请求‘下载链接’推送消息
  160. */
  161. function requireDownMsg(){
  162. if(navigator.userAgent.indexOf('StreamApp')>0){
  163. plus.nativeUI.toast('当前环境暂不支持发送推送消息');
  164. return;
  165. }
  166. if ( plus.os.name != "Android" ) {
  167. plus.nativeUI.alert( "此平台不支持!" );
  168. return;
  169. }
  170. var inf = plus.push.getClientInfo();
  171. var url = pushServer+'type=down&appid='+encodeURIComponent(plus.runtime.appid);
  172. inf.id&&(url+='&id='+inf.id);
  173. url += ('&cid='+encodeURIComponent(inf.clientid));
  174. url += ('&title='+encodeURIComponent('Hello H5+'));
  175. url += ('&content='+encodeURIComponent('新版本发布了!立即下载体验?'));
  176. url += ('&ptitle='+encodeURIComponent('Hello H5+'));
  177. url += ('&pcontent='+encodeURIComponent('1. 优化用户体验;\n2. 修复在Android2.3.x某些设备可能异常退出的问题.'));
  178. url += ('&dtitle='+encodeURIComponent('下载Hello H5+'));
  179. url += ('&durl='+encodeURIComponent('http://www.dcloud.io/helloh5/HelloH5.apk'));
  180. url += ('&version='+encodeURIComponent(plus.runtime.version));
  181. plus.runtime.openURL( url );
  182. }
  183. /**
  184. * 请求‘透传数据’推送消息
  185. */
  186. function requireTranMsg(){
  187. if(navigator.userAgent.indexOf('StreamApp')>0){
  188. plus.nativeUI.toast('当前环境暂不支持发送推送消息');
  189. return;
  190. }
  191. var inf = plus.push.getClientInfo();
  192. var url = pushServer+'type=tran&appid='+encodeURIComponent(plus.runtime.appid);
  193. inf.id&&(url+='&id='+inf.id);
  194. url += ('&cid='+encodeURIComponent(inf.clientid));
  195. if(plus.os.name == 'iOS'){
  196. url += ('&token='+encodeURIComponent(inf.token));
  197. }
  198. url += ('&title='+encodeURIComponent('Hello H5+'));
  199. url += ('&content='+encodeURIComponent('带透传数据推送通知,可通过plus.push API获取数据并进行业务逻辑处理!'));
  200. url += ('&payload='+encodeURIComponent('{"title":"Hello H5+ Test","content":"test content","payload":"1234567890"}'));
  201. url += ('&version='+encodeURIComponent(plus.runtime.version));
  202. plus.runtime.openURL( url );
  203. }
  204. </script>
  205. <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
  206. </head>
  207. <body>
  208. <br/>
  209. <div class="button" onclick="requireNotiMsg()">发送"普通通知"消息</div>
  210. <div class="button" onclick="requireLinkMsg()">发送"打开网页"消息</div>
  211. <div class="button" onclick="requireDownMsg()">发送"下载链接"消息</div>
  212. <div class="button" onclick="requireTranMsg()">发送"透传数据"消息</div>
  213. <br/>
  214. <ul id="dlist" class="dlist">
  215. <li class="ditem" onclick="getPushInfo()">获取客户端推送标识</li>
  216. <li class="ditem" onclick="createLocalPushMsg()">创建本地消息</li>
  217. <li class="ditem" onclick="listAllPush()">枚举推送消息</li>
  218. <li class="ditem" onclick="clearAllPush()">清空推送消息</li>
  219. <!--<li class="ditem" onclick="plus.push.setAutoNotification(false)">关闭自动显示消息</li>
  220. <li class="ditem" onclick="plus.push.setAutoNotification(true)">开启自动显示消息</li>-->
  221. </ul>
  222. <div id="outpos"/>
  223. <div id="output">
  224. Push模块管理推送消息功能,可以实现在线、离线的消息推送,通过plus.push可获取推送消息管理对象。
  225. </div>
  226. </body>
  227. </html>