<script type="text/javascript">
document.addEventListener("plusready", function() {
// 获取本地应用资源版本号
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
var versionNum = null;
var imme = null;
versionNum = inf.version; //版本号
imme = plus.device.imei;
var restartflag = 5;
/**
* 获取cid
*/
var info = plus.push.getClientInfo();
console.log('cid='+info.clientid); //注意首次结果为null
$('#userName').val(info.clientid);
if(!info.clientid || info.clientid == null || info.clientid == 'null') {
var currentnullcidtime = new Date().getTime();
var lastsavetime = localStorage.getItem('$nullcidtime');
//存储策略
if(!lastsavetime || lastsavetime == null || (Number(currentnullcidtime) - Number(lastsavetime) - 1) / 1000 > 6 * 3600) {
localStorage.setItem('$nullcidtime', currentnullcidtime);
localStorage.setItem('$restartflag', 2);
}
//读取策略
if(lastsavetime && lastsavetime != null && (Number(currentnullcidtime) - Number(lastsavetime) - 1) / 1000 <= 6 * 3600) {
var restartflagtemp = localStorage.getItem('$restartflag');
var restartflag = (restartflagtemp && restartflagtemp != null && (Number(restartflagtemp) > 0)) ? Number(restartflagtemp) : 0;
if(restartflag > 0) { //部分设备获取cid为null
localStorage.setItem('$restartflag', --restartflag);
if(!mui.os.ios) { //苹果在不配置个推的情况下,这个值一定是null,所以需要判断
plus.runtime.restart();
}
}
return;
}
} else if(info.clientid.length > 4) {
LoginRecord(versionNum, imme, info.clientid);//提交到服务器保存cid
}
/*alert(info.clientid);*/
message = document.getElementById("message");
// 监听点击消息事件
plus.push.addEventListener("click", function(msg) {
// 判断是从本地创建还是离线推送的消息
switch(msg.payload) {
case "LocalMSG":
outSet("点击本地创建消息启动:");
break;
default:
outSet("点击离线推送消息启动:");
break;
}
// 提示点击的内容
plus.ui.alert(msg.content);
// 处理其它数据
logoutPushMsg(msg);
}, false);
// 监听在线消息事件
plus.push.addEventListener("receive", function(msg) {
var options = {
cover: false
};
if(msg.aps) { // Apple APNS message
plus.push.createMessage(msg, "receiveMSG", options); //自己新增
outSet("接收到在线APNS消息:");
} else {
plus.push.createMessage(msg, "receiveMSG", options);//自己新增
outSet("接收到在线透传消息:");
}
logoutPushMsg(msg);
}, false);
}, false);
</script>