<!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 src="http://121.196.36.24:8001/Scripts/jquery-3.4.1.min.js"></script>
|
<script src="http://121.196.36.24:8001/Scripts/jquery.signalR-2.2.2.min.js"></script>
|
<script src="http://121.196.36.24:8001/SignalR/Hubs"></script>
|
<script type="text/javascript" src="../js/common.js"></script>
|
<script type="text/javascript">
|
var pushServer = "http://demo.dcloud.net.cn/push/?";
|
var message = null;
|
var message2 = 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 cont = $('#cont').val();
|
var options = {
|
cover: false
|
};
|
// var str = dateToStr(new Date());
|
// str += ": 欢迎使用HTML5+创建本地消息!";
|
// str = cont;
|
str = message2;
|
|
plus.push.createMessage(str, "LocalMSG", options);
|
outSet("创建本地消息成功!");
|
outLine("请到系统消息中心查看!");
|
if (plus.os.name == "iOS") {
|
outLine('*如果无法创建消息,请到"设置"->"通知"中配置应用在通知中心显示!');
|
}
|
|
// $.connection.hub.url = 'http://121.196.36.24:8001/signalr'
|
// //hub连接开启
|
// $.connection.hub.start().done(function() {
|
// var username = $('#displayname').val();
|
// //发送上线信息
|
// work.server.sendLogin(username);
|
|
// //点击按钮,发送消息
|
// $('#send').click(function() {
|
// var friend = $('#username').val();
|
// var cont = $('#cont').val();
|
// //调用后端函数,发送指定消息
|
// work.server.sendByGroup(username, friend, cont);
|
// });
|
// });
|
}
|
/**
|
* 读取所有推送消息
|
*/
|
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() {
|
//前端Hub的使用,注意的是,Hub的名字是ChatHub,这里使用时首字母小写
|
var work = $.connection.chatHub;
|
|
/*$('#displayname').val(prompt('请输入昵称:', ''));*/
|
$('#displayname').val("1");
|
$('#thisname').text('当前用户:' + $('#displayname').val());
|
|
|
|
//对应后端的SendMessage函数,消息接收函数
|
work.client.sendMessage = function(message) {
|
message2=message
|
console.log(message2)
|
$('#messgae').append(message + '</br>')
|
createLocalPushMsg()
|
};
|
|
|
|
//后端SendLogin调用后,产生的loginUser回调
|
work.client.loginUser = function(userlist) {
|
reloadUser(userlist);
|
};
|
|
|
$.connection.hub.url = 'http://121.196.36.24:8001/signalr'
|
//hub连接开启
|
$.connection.hub.start().done(function() {
|
var username = $('#displayname').val();
|
//发送上线信息
|
work.server.sendLogin(username);
|
|
//点击按钮,发送消息
|
$('#send').click(function() {
|
var friend = $('#username').val();
|
var cont = $('#cont').val();
|
//调用后端函数,发送指定消息
|
work.server.sendByGroup(username, friend, cont);
|
});
|
});
|
});
|
|
//重新加载用户列表
|
var reloadUser = function() {
|
$("#username").empty();
|
for (i = 0; i < 5; i++) {
|
$("#username").append("<option value=" + i + ">" + i + "</option>");
|
}
|
}
|
</script>
|
|
<link rel="stylesheet" href="../css/common.css" type="text/css" charset="utf-8" />
|
</head>
|
<body>
|
<h1>流程演示</h1>
|
<input type="hidden" id="displayname" />
|
<h2 id="thisname"></h2><br />
|
<select id="username" style="width:153px;">
|
</select>
|
<input id="cont" type="text" style="width:300px;">
|
<!-- <input id="send" type="button" value="发送" /> -->
|
<div>
|
<h1 id="messgae"></h1>
|
</div>
|
|
<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" id='send' onclick="createLocalPushMsg()">发送消息</li> -->
|
<li class="ditem" id='send'>发送消息</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>
|