先讲讲 用到的 cordova插件
cordova plugin add cordova-plugin-device cordova plugin add cordova-plugin-network-information cordova plugin add cordova-plugin-contacts //联系人
cordova plugin add D:\JobSourceCode\Mobile\Cordova-sqlite-storage-core-master // sqlite(这样使用是因为 网上的 sqlite 插件 有问题,自己 稍做修改)
dialog cordova plugin add cordova-plugin-vibration //震动
cordova plugin add cordova-plugin-file-transfer //文件传输
cordova plugin add cordova-plugin-camera //摄像头
//cordova plugin rm org.cordova-plugin-console
cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications //消息提示
cordova plugin add https://github.com/cordova-sms/cordova-sms-plugin.git //短信插件
//qq使用插件
cordova plugin add cordova-plugin-qqsdk --variable QQ_APP_ID=******
或者 cordova plugin add https://github.com/iVanPan/Cordova_QQ.git --variable QQ_APP_ID=******
或者 cordova plugin add D:\JobSourceCode\Mobile\Cordova_QQ-master\Cordova_QQ-master --variable QQ_APP_ID=******
cordova plugin add cordova-plugin-dialogs //消息提示,
cordova plugin add cordova-plugin-websocket //andori websocket(主要用户 用户之间 消息推送)
服务端采用 c# websocket
var UB_NotifySeverReciveMsg = {ReciveUserId:null,ReviceMsg:null,LoginUser:null,SourceType:"phone",NotifyFlag:null}; //var ReciveMsg = {Title:null,Text:null,AliasName:null,JsonStrData:null}; var UB_WEB_SOCKET = {_SERVER_URL:"ws://admin.uchengjiaoyi.com", _WEB_SOCKET:null, _CONECT_LOSE_NUM:0, _PORT:8000, IsRegisted:false, Init:function(){ var me = this; Ext.data.JsonP.request({ url: str_host + "/hd/hdAppSet.ashx?DoType=StartWebSocket", params:{JsonType:"JsonP"}, method:'POST', success: function(reinfo, opts) { if(reinfo.success){ me._PORT = reinfo.otherData; //alert(me._PORT); me._INIT_NEW(me._PORT); } else{ Ext.Msg.alert('信息提示!', reinfo.msg); } }, failure: function(result) { me._INIT_NEW(me._PORT); } }); }, _INIT_NEW:function(port){ var me = this; me._SERVER_URL = "ws://localhost"; if(me._WEB_SOCKET === null || me._WEB_SOCKET.readyState !== 1) { var websocket = new WebSocket(me._SERVER_URL + ":"+ port +"/Notifly"); me._WEB_SOCKET = websocket; websocket.onopen = function (e) { //处理连接上服务器 //onOpen(e); me._CONECT_LOSE_NUM = 0; me.RegistUser(); var notify_msg = me._NOTIFY_SHEET_SHOW("连接消息服务器成功!"); setTimeout(function(){ notify_msg.hide(); },1000); }; websocket.onmessage = function (e) { //处理获得的消息 //onMessage(e); var clilent_recive_msg = Ext.decode(e.data); if(clilent_recive_msg.DoType == "ReciveMsg"){ //接收消息 me._RECIVE_USERT_MSGS(clilent_recive_msg.ReciveData); } else if(clilent_recive_msg.DoType == "ReciveAction"){ //接收处理动作 me._RECIVE_ACTION_MSGS(clilent_recive_msg.ReciveData); } }; websocket.onerror = function (e) { //错误处理 if(me._WEB_SOCKET.readyState === 1) { var notify_msg = me._NOTIFY_SHEET_SHOW("消息通道错误!"); setTimeout(function(){ notify_msg.hide(); },1000); } }; websocket.onclose = function (e) { //关闭连接处理 me._CONECT_LOSE_NUM++; me.IsRegisted = false; if(me._CONECT_LOSE_NUM > 3){ me._NOTIFY_SHEET_SHOW("与消息服务器连接关闭!"); } else{ setTimeout(function(){ me.Init();},3000); } }; } }, _SEND_MSG:function(json_str){ var me = this; me._WEB_SOCKET.send(json_str); }, _SEND_MSG_SEVER:function(notifySeverReciveMsg){ var me = this; //数据签名 notifySeverReciveMsg.LoginUser = LoginInfo; var json_str = Ext.encode(notifySeverReciveMsg); me._SEND_MSG(json_str); }, _NOTIFY_SHEET_SHOW:function(msg){ var notify_msg = GetTC("MyAllMsgNotify"); var text_label = notify_msg.down("label[tag=websocket_msg]"); text_label.setHtml("<font color=red size=1>"+ msg +"</font>"); notify_msg.show(); return notify_msg; }, //接受到服务器的用户消息 _RECIVE_USERT_MSGS:function(msgs){ var me = this; for(var i = 0 ;i < msgs.length;i++){ var msg = msgs[i]; //保存到本地数据库 var sql = "insert into MY_MSG([ID],[TITLE],[MSG],[ALIAS_NAME],[JSON_DATA],"+ "[SEND_TIME],[FLAG],"+ "[HEAD_PATH],"+ "[USER_NAME],[USER_ID])"; sql += "values(?,?,?,?,?,?,?,?,?,?)"; ubdb.executeSql(sql,[msg.MsgID,msg.Title,msg.Text,msg.AliasName,msg.JsonStrData, msg.SendDate,'1', ((msg.SendUserHeadPath === null || msg.SendUserHeadPath === "" || msg.SendUserHeadPath === "null") ? "resources/images/userCenter_pic1.jpg":(str_host+msg.SendUserHeadPath)), msg.SendUserName,LoginInfo.FUCK_ID]); if (!Ext.os.is.Windows) { //本地推送 setTimeout(function(){ navigator.notification.vibrate(1000); cordova.plugins.notification.local.schedule({ id: msg.MsgID, title: msg.Title, text: msg.Text, // firstAt: monday_9_am, //every: "week", //icon: "http://www.easyicon.net/api/resizeApi.php?id=1184312&size=32", data: {aliasName:msg.AliasName,msgID:msg.MsgID,jsonData: (msg.JsonStrData && msg.JsonStrData !== "" && msg.JsonStrData !== "null") ? Ext.decode(msg.JsonStrData):""} }); },i*4000); } } if(msgs.length > 0){ me.ReflashLocalMsg(); } }, //接收到服务器的一些操作 _RECIVE_ACTION_MSGS:function(action){ var me = this; //接收到服务器端发回的成功登录 if(action.ActionType =="NotifyLogined") { me.IsRegisted = true; //alert("收到服务返回登录成功消息!"); } }, //刷新本地 消息列表 ReflashLocalMsg: function (){ var me = this; if(LoginInfo && current_window_name === "MyAllMsg"){ var component = GetTC("MyAllMsg"); var str_sql = "SELECT [ID],[TITLE],[MSG],[ALIAS_NAME],[JSON_DATA],"+ "[SEND_TIME],[FLAG],[HEAD_PATH],[USER_NAME],"+ "[USER_ID] FROM MY_MSG WHERE USER_ID = " + LoginInfo.FUCK_ID + " ORDER BY ID DESC"; ubdb.executeSql(str_sql, [], function(dt){ var store = component.down("list").getStore(); store.setData(dt.rows); }); } me.LoadMsgNum(); }, //主界面气泡显示 LoadMsgNum:function(){ if(current_window_name === "Main" ||current_window_name === "MyAllMsg" || current_window_name === "MyFriend"|| current_window_name === "MyHelp"){ ubdb.query("select count(0) as NUM from MY_MSG WHERE FLAG = '1' AND USER_ID = " + LoginInfo.FUCK_ID,[],function(data){ var img_msg_num = current_window.down("image[tag='img_msg_num']"); if(data.rows.length>0 && data.rows[0].NUM > 0){ img_msg_num.setHtml('<div style=" border:0px;width:20px; background-position:bottom; vertical-align:bottom; height:21px; color:White; background-image:url(\'resources/images/ball_red_24px.png\');">'+ data.rows[0].NUM +'</div>'); } else{ img_msg_num.setHtml(""); } }); } }, //从客户端向一个用户发送消息 SendUserMsg:function(reciveUserId,reviceMsg){ if(me._WEB_SOCKET !== null && me._WEB_SOCKET.readyState === 1) { var me = this; var notifySeverReciveMsg = Ext.extend(UB_NotifySeverReciveMsg,{}); notifySeverReciveMsg.ReviceMsg = reviceMsg; notifySeverReciveMsg.ReciveUserId = reciveUserId; notifySeverReciveMsg.NotifyFlag = "pushMsg"; notifySeverReciveMsg.SourceTyoe = "phone"; me._SEND_MSG_SEVER(notifySeverReciveMsg); return true; } else{ return false; } }, //登录后,注册用户,以便接收特定消息 RegistUser:function(){ var me = this; if(me._WEB_SOCKET !== null && me._WEB_SOCKET.readyState === 1 && me.IsRegisted === false) { var notifySeverReciveMsg = UB_NotifySeverReciveMsg; notifySeverReciveMsg.ReviceMsg = null; notifySeverReciveMsg.ReciveUserId = -999; notifySeverReciveMsg.NotifyFlag = "registPhone"; notifySeverReciveMsg.SourceTyoe = "phone"; me._SEND_MSG_SEVER(notifySeverReciveMsg); } }, DoMyMsg:function (msg_id){ Ext.data.JsonP.request({ url: str_host + "/hd/sys_manage.ashx?DoType=DoMsg", params:{MsgId:msg_id,JsonType:"JsonP"}, method:'POST', success: function(reinfo, opts) { if(!reinfo.success){ Ext.Msg.alert('信息提示!', reinfo.msg); } }, failure: function(result) { Ext.Msg.alert("处理我的信息失败!"); } }); } };