123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
- <meta name="HandheldFriendly" content="true"/>
- <meta name="MobileOptimized" content="320"/>
- <title>Hello H5+</title>
- <script type="text/javascript" src="../js/common.js"></script>
- <script type="text/javascript">
- var pushServer = "http://demo.dcloud.net.cn/push/?";
- var message = null;
- // 监听plusready事件
- document.addEventListener( "plusready", function(){
- message = document.getElementById("message");
- // 监听点击消息事件
- plus.push.addEventListener( "click", function( msg ) {
- // 判断是从本地创建还是离线推送的消息
- switch( msg.payload ) {
- case "LocalMSG":
- outSet( "点击本地创建消息启动:" );
- break;
- default:
- outSet( "点击离线推送消息启动:");
- break;
- }
- // 提示点击的内容
- plus.nativeUI.alert( msg.content );
- // 处理其它数据
- logoutPushMsg( msg );
- }, false );
- // 监听在线消息事件
- plus.push.addEventListener( "receive", function( msg ) {
- if ( msg.aps ) { // Apple APNS message
- outSet( "接收到在线APNS消息:" );
- } else {
- outSet( "接收到在线透传消息:" );
- }
- logoutPushMsg( msg );
- }, false );
- }, false );
- /**
- * 日志输入推送消息内容
- */
- function logoutPushMsg( msg ) {
- outLine( "title: "+msg.title );
- outLine( "content: "+msg.content );
- if ( msg.payload ) {
- if ( typeof(msg.payload)=="string" ) {
- outLine( "payload(String): "+msg.payload );
- } else {
- outLine( "payload(JSON): "+JSON.stringify(msg.payload) );
- }
- } else {
- outLine( "payload: undefined" );
- }
- if ( msg.aps ) {
- outLine( "aps: "+JSON.stringify(msg.aps) );
- }
- }
- /**
- * 获取本地推送标识信息
- */
- function getPushInfo(){
- outSet("获取客户端推送标识信息:");
- plus.push.getClientInfoAsync(function(info){
- outLine('Success');
- outLine(JSON.stringify(info));
- }, function(e){
- outLine('Failed');
- outLine(JSON.stringify(e));
- });
- }
- /**
- * 本地创建一条推动消息
- */
- function createLocalPushMsg(){
- var options = {cover:false};
- var str = dateToStr(new Date());
- str += ": 欢迎使用HTML5+创建本地消息!";
- plus.push.createMessage( str, "LocalMSG", options );
- outSet( "创建本地消息成功!" );
- outLine( "请到系统消息中心查看!" );
- if(plus.os.name=="iOS"){
- outLine('*如果无法创建消息,请到"设置"->"通知"中配置应用在通知中心显示!');
- }
- }
- /**
- * 读取所有推送消息
- */
- function listAllPush(){
- var msgs=null;
- switch ( plus.os.name ) {
- case "Android":
- msgs = plus.push.getAllMessage();
- break;
- default:
- break;
- }
- if ( !msgs ) {
- outSet( "此平台不支持枚举推送消息列表!" );
- return;
- }
- outSet( "枚举消息列表("+msgs.length+"):" );
- for ( var i in msgs ) {
- var msg = msgs[i];
- outLine( i+": "+msg.title+" - "+msg.content );
- }
- }
- /**
- * 清空所有推动消息
- */
- function clearAllPush(){
- plus.push.clear();
- outSet( "清空所有推送消息成功!" );
- }
- /**
- * 请求‘简单通知’推送消息
- */
- function requireNotiMsg(){
- if(navigator.userAgent.indexOf('StreamApp')>0){
- plus.nativeUI.toast('当前环境暂不支持发送推送消息');
- return;
- }
- var inf = plus.push.getClientInfo();
- var url = pushServer+'type=noti&appid='+encodeURIComponent(plus.runtime.appid);
- inf.id&&(url+='&id='+inf.id);
- url += ('&cid='+encodeURIComponent(inf.clientid));
- if(plus.os.name == 'iOS'){
- url += ('&token='+encodeURIComponent(inf.token));
- }
- url += ('&title='+encodeURIComponent('Hello H5+'));
- url += ('&content='+encodeURIComponent('欢迎回来体验HTML5 plus应用!'));
- url += ('&version='+encodeURIComponent(plus.runtime.version));
- plus.runtime.openURL( url );
- }
- /**
- * 请求‘打开网页’推送消息
- */
- function requireLinkMsg(){
- if(navigator.userAgent.indexOf('StreamApp')>0){
- plus.nativeUI.toast('当前环境暂不支持发送推送消息');
- return;
- }
- var inf = plus.push.getClientInfo();
- var url = pushServer+"type=link&appid="+encodeURIComponent(plus.runtime.appid);
- inf.id&&(url+='&id='+inf.id);
- url += ('&cid='+encodeURIComponent(inf.clientid));
- if(plus.os.name == 'iOS'){
- url += ('&token='+encodeURIComponent(inf.token));
- }
- url += ('&title='+encodeURIComponent('HBuilder飞一样的编码'));
- url += ('&content='+encodeURIComponent('看HBuilder如何追求代码编写速度的极致!立即去瞧一瞧?'));
- url += ('&url='+encodeURIComponent('http://www.dcloud.io/'));
- url += ('&version='+encodeURIComponent(plus.runtime.version));
- plus.runtime.openURL( url );
- }
- /**
- * 请求‘下载链接’推送消息
- */
- function requireDownMsg(){
- if(navigator.userAgent.indexOf('StreamApp')>0){
- plus.nativeUI.toast('当前环境暂不支持发送推送消息');
- return;
- }
- if ( plus.os.name != "Android" ) {
- plus.nativeUI.alert( "此平台不支持!" );
- return;
- }
- var inf = plus.push.getClientInfo();
- var url = pushServer+'type=down&appid='+encodeURIComponent(plus.runtime.appid);
- inf.id&&(url+='&id='+inf.id);
- url += ('&cid='+encodeURIComponent(inf.clientid));
- url += ('&title='+encodeURIComponent('Hello H5+'));
- url += ('&content='+encodeURIComponent('新版本发布了!立即下载体验?'));
- url += ('&ptitle='+encodeURIComponent('Hello H5+'));
- url += ('&pcontent='+encodeURIComponent('1. 优化用户体验;\n2. 修复在Android2.3.x某些设备可能异常退出的问题.'));
- url += ('&dtitle='+encodeURIComponent('下载Hello H5+'));
- url += ('&durl='+encodeURIComponent('http://www.dcloud.io/helloh5/HelloH5.apk'));
- url += ('&version='+encodeURIComponent(plus.runtime.version));
- plus.runtime.openURL( url );
- }
- /**
- * 请求‘透传数据’推送消息
- */
- function requireTranMsg(){
- if(navigator.userAgent.indexOf('StreamApp')>0){
- plus.nativeUI.toast('当前环境暂不支持发送推送消息');
- return;
- }
- var inf = plus.push.getClientInfo();
- var url = pushServer+'type=tran&appid='+encodeURIComponent(plus.runtime.appid);
- inf.id&&(url+='&id='+inf.id);
- url += ('&cid='+encodeURIComponent(inf.clientid));
- if(plus.os.name == 'iOS'){
- url += ('&token='+encodeURIComponent(inf.token));
- }
- url += ('&title='+encodeURIComponent('Hello H5+'));
- url += ('&content='+encodeURIComponent('带透传数据推送通知,可通过plus.push API获取数据并进行业务逻辑处理!'));
- url += ('&payload='+encodeURIComponent('{"title":"Hello H5+ Test","content":"test content","payload":"1234567890"}'));
- url += ('&version='+encodeURIComponent(plus.runtime.version));
- plus.runtime.openURL( url );
- }
- </script>
- <link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8"/>
- </head>
- <body>
- <br/>
- <div class="button" onclick="requireNotiMsg()">发送"普通通知"消息</div>
- <div class="button" onclick="requireLinkMsg()">发送"打开网页"消息</div>
- <div class="button" onclick="requireDownMsg()">发送"下载链接"消息</div>
- <div class="button" onclick="requireTranMsg()">发送"透传数据"消息</div>
- <br/>
- <ul id="dlist" class="dlist">
- <li class="ditem" onclick="getPushInfo()">获取客户端推送标识</li>
- <li class="ditem" onclick="createLocalPushMsg()">创建本地消息</li>
- <li class="ditem" onclick="listAllPush()">枚举推送消息</li>
- <li class="ditem" onclick="clearAllPush()">清空推送消息</li>
- <!--<li class="ditem" onclick="plus.push.setAutoNotification(false)">关闭自动显示消息</li>
- <li class="ditem" onclick="plus.push.setAutoNotification(true)">开启自动显示消息</li>-->
- </ul>
- <div id="outpos"/>
- <div id="output">
- Push模块管理推送消息功能,可以实现在线、离线的消息推送,通过plus.push可获取推送消息管理对象。
- </div>
- </body>
- </html>
|