public void Send(MessagesAddRequest receivedMessage, string receiverUserId) { // get connection id of logged in user var getConnectionId = Context.ConnectionId; // get user id var userId = Context.User.Identity.GetUserId(); // declare receiver ChatUser obj ChatUser receiver; // get receiver information with passed in receiver guid (user id) if (MyUsers.TryGetValue(receiverUserId, out receiver)) { // get connected user ChatUser sender = GetUser(userId); // declare IEnumerable string for all connection ids IEnumerable <string> allReceivers; // lock ensures that one thread does not enter a critical section of code // while another thread is in the critical section. If another thread tries // to enter a locked code, it will wait, block, until the object is released. lock (receiver.ConnectionIds) { lock (sender.ConnectionIds) { // combine receiver and sender connection ids allReceivers = receiver.ConnectionIds.Concat(sender.ConnectionIds); } } // loop through each id and broadcast to all connection ids foreach (var cid in allReceivers) { // Broadcast to every other connection ID except current user // ** handle pushed messaged for current user on the front end (postMessageSuccess) if (cid != getConnectionId) { Clients.Client(cid).broadcastMessage(new { //set the variables to the received data sender = sender.ChatUserId, rMessage = receivedMessage }); } } } }
public HttpResponseMessage CreateMessage(MessagesAddRequest payload) { try { itemResponse.Item = _messagingService.CreateMessage(payload); return(Request.CreateResponse(itemResponse)); } catch (Exception ex) { ErrorLogService svc = new ErrorLogService(); ErrorLogAddRequest error = new ErrorLogAddRequest(); error.ErrorFunction = "Sabio.Web.Controllers.Api.CreateMessage"; error.ErrorMessage = ex.Message; error.UserId = UserService.UserSelect().PersonId; svc.ErrorLogInsert(error); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message)); } }
/// <summary> /// 推送1-n个用户(用户别名) /// </summary> /// <param name="pushMessageInfo">推送信息实体</param> private void PushMessageToList(PushMessageInfo pushMessageInfo) { int isMute = 0;//设定是否静音,只在于推送 客户等于1时可用 IGtPush push = new IGtPush(HOST, APPKEY, MASTERSECRET); string content = pushMessageInfo.Contents; string title = string.Empty; var transmissionContent = new { Template = pushMessageInfo.Template, Content = pushMessageInfo.Contents, Url = pushMessageInfo.Url, Section = pushMessageInfo.TypeID + "/" + pushMessageInfo.Section, NodeId = pushMessageInfo.DataID }; string jsonContent = JsonHelper.ConvertToJson(transmissionContent); PushLog("- - - - - - - - - - - - - "); PushLog(string.Format("推送类型:{0}", pushMessageInfo.PushType)); PushLog(string.Format("推送内容:{0}", pushMessageInfo.Contents)); PushLog(string.Format("推送目标:{0}", pushMessageInfo.TargetList)); if (pushMessageInfo.TypeID != 0) { title = Enum.GetName(typeof(SectionSecondType), pushMessageInfo.Section); } //设置接收者 List <com.igetui.api.openservice.igetui.Target> targetList = new List <com.igetui.api.openservice.igetui.Target>(); if (pushMessageInfo.TargetList.IndexOf(",") == -1) { pushMessageInfo.TargetList += ","; } string[] targets = pushMessageInfo.TargetList.Split(','); for (int i = 0; i < targets.Length; i++) { if (targets[i] != null && targets[i] != string.Empty && targets[i] != "") { com.igetui.api.openservice.igetui.Target target = new com.igetui.api.openservice.igetui.Target(); target.appId = APPID; target.alias = targets[i]; #region 判定是否允许推送,并向java接口写入推送信息 MessagesAddBLL pushBLL = new MessagesAddBLL(); MessagesAddRequest maRequest = new MessagesAddRequest(); maRequest.creator = "推送程序"; maRequest.userID = Convert.ToInt32(target.alias); maRequest.status = 0; if (pushMessageInfo.Template == 1) { maRequest.contents = pushMessageInfo.Contents; } else { maRequest.contents = pushMessageInfo.Url; } if (pushMessageInfo.TypeID != 0) { maRequest.typeID = pushMessageInfo.TypeID; } if (pushMessageInfo.Section != 0) { maRequest.dataTypeID = pushMessageInfo.Section; } maRequest.dataID = pushMessageInfo.DataID; //写入至未读消息列表 var respon = pushBLL.MessagesAdd(maRequest); //IOS 数据判定开关 if (pushMessageInfo.ChannelID == 1 || pushMessageInfo.ChannelID == 0) { NotifyConfigGetBLL notifyConfigGetBLL = new NotifyConfigGetBLL(); NotifyConfigGetRequest request = new NotifyConfigGetRequest(); request.userID = maRequest.userID; request.userPlatform = 3; NotifyConfigGetResponse ncgResponse = notifyConfigGetBLL.NotityConfigGet(request); //转化为2进制字符串后换算出是否推送 //string allStatus = Convert.ToString(1, 2); string allStatus = Convert.ToString(ncgResponse.notifyConfig.notifySwitch, 2); int t = GetIntegerSomeBit(ncgResponse.notifyConfig.notifySwitch, maRequest.typeID - 1); if (targets.Length == 2) { //静音的二进制返回位数 isMute = GetIntegerSomeBit(ncgResponse.notifyConfig.notifySwitch, 4); } //包含用户已有权限+不存在的用户 if (t == 0) { if (respon.code == 0) { targetList.Add(target); } else { PushLog(string.Format("消息写入失败,无法推送:{0}", maRequest.userID)); } } else { PushLog(string.Format("跳过推送ID:{0}", maRequest.userID)); } } else //Android { if (respon.code == 0) { targetList.Add(target); } else { PushLog(string.Format("消息写入失败,无法推送:{0}", maRequest.userID)); } } #endregion } } if (targetList == null || targetList.Count == 0) { return; } #region 因为IOS端需要静音,透传模板放置在方法底部 ListMessage message = new ListMessage(); //设置透传模板 TransmissionTemplate template = TransmissionTemplateDemo(content, title, jsonContent, isMute); template.TransmissionType = "2"; //应用启动类型,1:强制应用启动 2:等待应用启动 template.TransmissionContent = jsonContent; //透传内容 message.IsOffline = true; message.OfflineExpireTime = 1000 * 3600 * 12; message.Data = template; #endregion string contentId = push.getContentId(message); string pushResult = push.pushMessageToList(contentId, targetList); try { var result = JsonHelper.ConvertToObj <PushResult>(pushResult); PushLog("推送结果:" + result.result); } catch { PushLog("推送结果转换异常"); } }
private static string add = "messages/addMessages"; //添加通知信息 public MessagesAddResposne MessagesAdd(MessagesAddRequest request) { string postData = JsonHelper.GetJson(request); return(client.Execute(request, add, postData)); }