Esempio n. 1
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;
        }
        // PUT api/Messages/5
        public HttpResponseMessage PutMessage(long id, Message message)
        {
            if (ModelState.IsValid && id == message.Id)
            {
                db.Entry(message).State = EntityState.Modified;

                try
                {
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return Request.CreateResponse(HttpStatusCode.NotFound);
                }

                return Request.CreateResponse(HttpStatusCode.OK);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }
        }
 public ActionResult Edit(Message message)
 {
     if (ModelState.IsValid)
     {
         db.Entry(message).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     return View(message);
 }