public bool NewIosPush(IosPushModel objPush) { try { string p12Certificate = "WizzDevelopement.p12"; #if DEBUG // p12Certificate = "WizzProductioncert.p12"; #else p12Certificate = "WizzProductioncert.p12"; #endif string p12fileName = p12Certificate; string password = "******"; X509Certificate2Collection certs = new X509Certificate2Collection(); // Add the Apple cert to our collection certs.Add(getServerCert(p12fileName, password)); string apsHost; if (getServerCert(p12fileName, password).ToString().Contains("Production")) apsHost = "gateway.push.apple.com"; // Port= Convert.ToInt32("2195") else apsHost = "gateway.sandbox.push.apple.com"; TcpClient tcpClient = new TcpClient(apsHost, 2195); // Connect(); // Create a TCP socket connection to the Apple server on port 2195 // Create a new SSL stream over the connection sslStream = new SslStream(tcpClient.GetStream()); // Authenticate using the Apple cert sslStream.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false); //6455903fb51593b5fb930ab8184e39c5ec83b57503e6589596d463ddb4b1d835 if (objPush.Message.Length < 256) { Boolean send = PushIosMessage(objPush); return send; } else { throw new Exception("Message not exeed 256 character"); } } catch (Exception ex) { return false; // throw new Exception(ex.Message); } }
public string SendPushForIos(IosPushModel objPush) { try { // msg = "order confirmed"; WebRequest tRequest; tRequest = WebRequest.Create("https://android.googleapis.com/gcm/send"); tRequest.Method = "post"; tRequest.ContentType = "application/x-www-form-urlencoded"; string resistrationId = objPush.deviceToken; string subjectName = objPush.subjectName; var key = ""; key = ConfigurationManager.AppSettings["androidPushKey"]; tRequest.Headers.Add(string.Format("Authorization: key={0}", key)); String collaspeKey = Guid.NewGuid().ToString("n"); // string messagetosend = // "{ \"aps\" :{ \"alert\" : \""+ objPush.message+"\" }, \"type\" : \""+objPush.type+"\"}"; // string messagetosend = "{ \"aps\" :{ \"alert\" : \"" + objPush.message + "\" , \"type\" : \"" + objPush.type + "\", \"sessionId\" : \"" + objPush.sessionId + "\"}}"; string messagetosend = "{\"aps\":{\"alert\":\"" + objPush.senderName + " " + objPush.Message + "\",\"badge\":\"1\",\"sound\":\"default\",\"type\":\"" + objPush.Type + "\",\"subjectName\":\"" + objPush.subjectName + "\",\"senderId\":\"" + objPush.senderId + "\",\"messageId\":\"" + objPush.messageId + "\"}}"; // "{\"aps\":{\"alert\":\"" + "hi" + "\",\"badge\":" +0.ToString() + ",\"sound\":\"default\",\"Tag\":\"" + "1" + "\"}}"; ; String postData = string.Format("registration_id={0}&data.payload={1}&collapse_key={2}&sessionId={3}&amount={3}", resistrationId, "" + messagetosend, collaspeKey, objPush.subjectName, objPush.senderId); Byte[] byteArray = Encoding.UTF8.GetBytes(postData); tRequest.ContentLength = byteArray.Length; Stream dataStream = tRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse tResponse = tRequest.GetResponse(); dataStream = tResponse.GetResponseStream(); StreamReader tReader = new StreamReader(dataStream); String sResponseFromServer = tReader.ReadToEnd(); tReader.Close(); dataStream.Close(); tResponse.Close(); return sResponseFromServer; } catch (Exception ex) { return null; } }
//internal bool AcceptStudent(StudentTeacherMap objReq) //{ // // var isTutor = Convert.ToBoolean(objReq.userId); // using (var db = new WizzDataContext()) // { // if (objReq.isAccept.ToLower() == "true") // { // tblRequest reqStudent = new tblRequest(); // var studentList = db.tblRequestStudents.Where(x => x.isAccepted != true && x.isDelete == false && x.uniqueRequestId == objReq.uniqueRequestId && x.fkUserId == Convert.ToInt32(objReq.tutorId)).ToList(); // studentList.ForEach(a => { a.isAccepted = true; a.updatedDate = DateTime.UtcNow; }); // db.SubmitChanges();//update student request // var totalList = db.tblRequests.Where(x => x.isAccepted != true && x.uniqueRequestId == objReq.uniqueRequestId && x.fkStudentId==Convert.ToInt32(objReq.studentId)).ToList(); // var deleteList = totalList.Where(x => x.fkTutorId != Convert.ToInt32(objReq.tutorId)).ToList(); // //delete all tutors who recived the notiifcation except the tutor who is accepeting the notification // reqStudent = totalList.Where(x => x.fkTutorId == Convert.ToInt32(objReq.tutorId)).FirstOrDefault(); // db.tblRequests.DeleteAllOnSubmit(deleteList); // db.SubmitChanges(); // //accept the request // if (reqStudent == null)//if request is not exist // return false; // reqStudent.isAccepted = true; // reqStudent.updatedDate = DateTime.UtcNow; // var userData = db.tblUsers.Where(x => x.pkUserId == Convert.ToInt32(objReq.studentId)&& x.isTeacher==false).FirstOrDefault(); // var teacherData = db.tblUsers.Where(x => x.pkUserId == Convert.ToInt32(objReq.tutorId)).FirstOrDefault(); // if (userData!=null &&!string.IsNullOrEmpty(userData.deviceToken)) // { // // objPush.type = Convert.ToInt16(PushType.acceptRequest).ToString(); // PushData push = new PushData() // { // registration_ids = userData.deviceToken, // //push.data = CommonMethod.ObjectToJson(objPush); // message = PushMessages.FormatMessage(PushMessages.acceptRequest, teacherData.userName), // type = Convert.ToInt16(PushType.acceptRequest).ToString(), // badge = Convert.ToString(userData.badge) // }; // if (userData.deviceType == Convert.ToInt16(DeviceType.ios))//ios // { // SendPush objIOS = new SendPush(); // objIOS.ConnectToAPNS(push); // } // else if (userData.deviceType == Convert.ToInt16(DeviceType.android))//android // { // Android objAndroid = new Android(); // objAndroid.send(push); // } // } // // update tblrequest by isAccept=1 and delete rest of request // } // else // { // var objRequestData = db.tblRequests.Where(x => x.isAccepted == false && x.fkTutorId ==Convert.ToInt32(objReq.tutorId) && x.uniqueRequestId==objReq.uniqueRequestId).FirstOrDefault(); // if (objRequestData != null) // { // db.tblRequests.DeleteOnSubmit(objRequestData); // db.SubmitChanges(); // } // else { // return false; // } // } // db.SubmitChanges(); // return true; // } //} #endregion internal bool SendPushForIos(IosPushModel push) { tblUser receiverData = new tblUser(); using (var db = new WizzDataContext()) { receiverData = db.tblUsers.Where(c => c.pkUserId == Convert.ToInt32(push.receiverId)).FirstOrDefault(); var sendor = db.tblUsers.Where(c => c.pkUserId == Convert.ToInt32(push.senderId)).FirstOrDefault(); // objPush.deviceToken = receiverData.deviceToken; PushData objPush = new PushData() { message = sendor.userName + ": " + push.Message, type = Convert.ToInt16(PushType.chatPush).ToString(), messageType=push.Type, userId=push.senderId, subject=push.subjectName, registration_ids = receiverData.deviceToken, badge = Convert.ToString(receiverData.badge), uniqueRequestid=push.requestId }; // push.type = PushType.newRequest.ToString(); // push.data = CommonMethod.ObjectToJson(objPush); // push.Type =push.Type; // push.messageId = push.messageId; // push.Message = push.Message; // push.senderName=push if (receiverData.deviceType == 1)//ios { SendPush objIOS = new SendPush(); objIOS.ConnectToAPNS(objPush); } else if (receiverData.deviceType == 2)//android { PushForIos objAndroid = new PushForIos(); objAndroid.SendPushForIos(push); } } return true; ///throw new NotImplementedException(); }
private bool PushIosMessage(IosPushModel objPush) { try { ; // String cAlert = objPush.data; int iBadge = 0; // Ready to create the push notification byte[] buf = new byte[256]; MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write(new byte[] { 0, 0, 32 }); byte[] deviceToken = HexToData(objPush.deviceToken); bw.Write(deviceToken); bw.Write((byte)0); // Create the APNS payload - new.caf is an audio file saved in the application bundle on the device //string msg = "{\"aps\":{\"alert\":\"" + cAlert + "\",\"badge\":" + iBadge.ToString() + ",\"sound\":\"default\",\"Tag\":\"" + tag + "\"}}"; // string msg = "{\"aps\":{\"alert\":\"" + objPush.message + "\",\"badge\":\"1\",\"sound\":\"default\",\"type\":" + objPush.type + ",\"sessionId\":" + objPush.sessionId + ",\"userId\":" + objPush.userId + ",\"amount\":" + objPush.amount + "}}"; string msg = "{\"aps\":{\"alert\":\"" + objPush.senderName + " " + objPush.Message + "\",\"badge\":\"" + objPush.badge + "\",\"sound\":\"default\",\"type\":\"" + objPush.Type + "\",\"messageId\":\"" + objPush.messageId + "\",\"senderId\":\"" + objPush.senderId + "\",\"subjectName\":\"" + objPush.subjectName + "\"}}"; // Write the data out to the stream bw.Write((byte)msg.Length); bw.Write(msg.ToCharArray()); bw.Flush(); if (sslStream != null) { sslStream.Write(ms.ToArray()); return true; } return false; } catch (Exception ex) { throw new Exception(ex.Message); } }
public Response<string> SendPushForIos(IosPushModel objReq) { Response<string> response = new Response<string>(); List<string> objResp = new List<string>(); try { WebMethods obj = new WebMethods(); if (CheckRequestIsvalidornot(this.Request)) { if (obj.SendPushForIos(objReq)) { response.Create(true, "Message sent", Messages.AppVersion, objResp); } else response.Create(false, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage, Messages.AppVersion, objResp); } else response.Create(false, Messages.FormatMessage(Messages.InvalidReq), Messages.AppVersion, objResp); } catch (Exception ex) { object session = new JavaScriptSerializer().Serialize(objReq); LogManager.Error("Error occured while Processing Webservice request :{0}", ex, session, ex.Message); response.Create(false, Messages.FormatMessage(Messages.ErrorOccure), Messages.AppVersion, objResp); } finally { } return response; }