public ResponseModel SendNoti(NotificationSendModel model) { var response = new ResponseModel { Success = false, Messages = new List<string>() }; if (model == null || model.AppId==0) { response.Messages.Add("Mandatory data can not be empty. AppId can not be 0."); response.Data = model; } // else if (model.Action!= NotificationEnum.testing || (model.UserIds == null || model.UserIds.Count == 0) || string.IsNullOrEmpty(model.Text)) { response.Messages.Add("You need to add UserIds for notifications with proper Action & Text "); response.Data = model; } else { try { var res = NotificationService.SendNotification(model); response.Data = res; response.Messages.Add("SUCCESS"); response.Success = true; } catch (Exception excep) { response.Messages.Add("Something bad happened."); } } return response; }
public static List <SendNotiResponseModel> SendNotification(NotificationSendModel model) { var response = new List <SendNotiResponseModel>(); using (var dbContext = new DeliversEntities()) { //#region Confirmed //if (model.Action == NotificationEnum.confirmed) //{ // var allDevliveryBoyes = dbContext.AspNetUsers.Where(o => o.Type == 0 && (o.Status ?? false) && (o.IsApproved ?? false)).ToList(); // if (allDevliveryBoyes != null && allDevliveryBoyes.Any()) // { // model.UserIds = new List<string>(); // model.UserIds = allDevliveryBoyes.Select(o => o.Id.ToString()).ToList(); // } //} //if(model.UserIds==null || !model.UserIds.Any()) //{ // var tempoRes = new SendNotiResponseModel // { // UserId = "0", // AppId = 0000, // Token = new List<string> { "NOTHING"}, // Response = "No active delivery boy found" // }; // response.Add(tempoRes); // return response; //} //#endregion foreach (var u in model.UserIds) { var tempoRes = new SendNotiResponseModel { UserId = u, AppId = model.AppId, Token = new List <string>(), Response = "" }; var tokens = new List <Token>(); var user = dbContext.AspNetUsers.FirstOrDefault(obj => obj.Id == u); if (user != null) { var toks = dbContext.Tokens.Where(t => t.UserId == user.Id && t.AppId == model.AppId); if (toks != null && toks.Any()) { tokens.AddRange(toks); foreach (var tok in tokens) { tempoRes.Token.Add(tok.Token1); tempoRes.Response = SendNotiInner(model.AppId, tok.Token1, model.Text, "Testing"); var tempoNoti = new Notification { AppId = model.AppId, DateTime = CommonService.GetSystemTime(), Text = model.Text + " at " + CommonService.GetSystemTime().ToLongTimeString(), UserId = user.Id }; dbContext.Notifications.Add(tempoNoti); dbContext.SaveChanges(); } } else { tempoRes.Token.Add("User is valid but No Token Found for this user"); } } else { tempoRes.Token.Add("User is not valid"); } response.Add(tempoRes); } } return(response); }