public ActionResult Create(ViewMessage message)
 {
     var ret = string.Empty;
     if (ModelState.IsValid)
     {
        var resp= MessageLogic.SendMessage(message);
        switch (resp.status_code)
        {
            case "200":
                Success(@"Message Sent Successfully");
                break;
            case "210":
                Error(@"Argument Error, " + resp.status_message);
                break;
            case "N/A":
                Error(@"Error!, Malformed request string");
                break;
            case "500":
                Error( @"Internal Error Occured");
                break;
        }
        // return RedirectToAction("Index");
     }else
     Error("there were some errors in your form.");
     return View(message);
 }
        // POST api/Messages
        public HttpResponseMessage PostMessage(ViewMessage message)
        {
            if (ModelState.IsValid)
            {
               var resp= MessageLogic.SendMessage(message);
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created,resp);

                return response;
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
Example #3
0
        internal static MessageResponse SendMessage(ViewMessage msg)
        {
            string apiToken = ConfigurationManager.AppSettings["ApiToken"];
             string applicationCode = ConfigurationManager.AppSettings["ApplicationCode"];
             List<string> deviceTokens = new List<string>();
             List<string> phoneList = new List<string>();

             Message newMessage = new Message();

             newMessage.MessageText = msg.MessageText;

             if (msg.ToAll)
             {
                 var devices = repo.Select<Device>().ToList();
                 foreach (Device dv in devices)
                 {

                     string token = dv.DeviceToken;
                     deviceTokens.Add(token);
                     phoneList.Add(dv.DevicePhoneNumber);
                 }

             }
             else
             {
                 string[] phoneNo = msg.PhoneNumbers.Split(',');
                 foreach (string item in phoneNo)
                 {
                     string item1 = Md5.GetMd5Hash(item);
                     var firstOrDefault = repo.Select<Device>().FirstOrDefault(q => q.DevicePhoneNumber == item1);
                     if (firstOrDefault != null)
                     {

                         string token = firstOrDefault.DeviceToken;
                         deviceTokens.Add(token);
                         phoneList.Add(item);
                     }
                 }
             }
            // req.request.notifications.devices = new List<string>(deviceTokens.ToArray());

             //MessageResponse resp = PushService.DoSendMessage(req);

             JObject json = new JObject(
                 new JProperty("application", applicationCode),
                 new JProperty("auth", apiToken),
                 new JProperty("notifications",
                     new JArray(
                         new JObject(
                             new JProperty("send_date", "now"),
                             new JProperty("content", msg.MessageText),
                             new JProperty("wp_type", "Toast"),
                             new JProperty("wp_count", 3),
                             new JProperty("data",
                                 new JObject(
                                     new JProperty("custom", "json data"))),
                                     new JProperty("platforms", new JArray(new List<int> { 1, 2, 3, 4, 5 })),
                            // new JProperty("link", "http://Vanso.com/"),
                            // new JProperty("conditions",
                              //   new JArray(
                                //     (object)new JArray("Color", "EQ", "black")))
                                new JProperty("devices",new JArray(new List<string>(deviceTokens.ToArray())))
                                     ))));

            var resp= PushService.SendPush("createMessage", json);

                 newMessage.DateSent = DateTime.Now;
                 newMessage.MessageStatus = resp.status_message;
                 newMessage.StatusCode = int.Parse(resp.status_code);
                 if (resp.status_code != "200")
                 {
                     newMessage.MessageId = "0";
                 }
                 else
                 {
                     newMessage.MessageId = resp.response.messages[0];
                 }
             newMessage.PhoneNumber = JsonConvert.SerializeObject(phoneList);
                repo.Insert(newMessage);

             return resp;
        }