public void Execute(IJobExecutionContext context) { PushNotification fcm = new PushNotification(); TittleNotificationServices service = new TittleNotificationServices(); //get all the active notifications List <notification> notifications = service.GetActiveNotifications(); //loop through notifications if (notifications != null) { for (int i = 0; i < notifications.Count; i++) { string message = notifications[i].content; //get list of users in notification List <CustomNotificationUser> users = service.GetNotificationUsersList(notifications[i].id); if (users != null && users.Count > 0) { //loop through users for (int j = 0; j < users.Count; j++) { //save in notification box notification_boxes nb = new notification_boxes(); nb.type = 10; nb.device_id = users[j].id; nb.device_type = "App\\Models\\User"; nb.message = message; nb.task_id = 0; nb.unread = ""; nb.seen = 0; nb.kid_id_ref = 0; service.SaveNotificationBoxInfo(nb); //send notification one by one to devices List <device> devices = service.GetListOfDevices(users[j].id); for (int k = 0; k < devices.Count; k++) { fcm.Send(10, message, devices[k].id); } } } //update notification table if (notifications[i].type == "onetime") { notifications[i].status = "completed"; } else { if (notifications[i].type == "daily") { notifications[i].next_notification = ((DateTime)notifications[i].next_notification).AddDays(1); } else if (notifications[i].type == "weekly") { notifications[i].next_notification = ((DateTime)notifications[i].next_notification).AddDays(7); } else if (notifications[i].type == "monthly") { notifications[i].next_notification = ((DateTime)notifications[i].next_notification).AddMonths(1); } } service.UpdateNotificationInfo(notifications[i]); } } }
public ActionResult PushNotifications(CustomNotify model) { if (Session["UserID"] != null && ModelState.IsValid) { PushNotification fcm = new PushNotification(); TittleUserServices service = new TittleUserServices(); TittleNotificationServices nservice = new TittleNotificationServices(); List <string> emails = null; if (model.Type == "immediately") { if (!string.IsNullOrEmpty(model.To)) { emails = model.To.Split('|').ToList(); } else { emails = service.GetAllUsersEmail(); } //loop through users and send notification foreach (string email in emails) { List <CustomNotificationUser> users = nservice.GetUserDetailByEmail(email); for (int j = 0; j < users.Count; j++) { //save in notification box notification_boxes nb = new notification_boxes(); nb.type = 10; nb.device_id = users[j].id; nb.device_type = "App\\Models\\User"; nb.message = model.Content; nb.task_id = 0; nb.unread = ""; nb.seen = 0; nb.kid_id_ref = 0; nservice.SaveNotificationBoxInfo(nb); //send notification one by one to devices List <device> devices = nservice.GetListOfDevices(users[j].id); for (int k = 0; k < devices.Count; k++) { fcm.Send(10, model.Content, devices[k].id); } } } } else { CustomNotification not = new CustomNotification(); if (!string.IsNullOrEmpty(model.To)) { not.data = "{\"users\":\"" + string.Join(",", model.To.Split('|')) + "\"}"; } else { not.data = "{\"users\":\"all\"}"; } not.content = model.Content; not.name = string.IsNullOrEmpty(model.Name)? "Unknown": model.Name; not.OnDate = model.TimeStart.HasValue ? model.TimeStart.Value.ToString("dd/MM/yyyy hh:mm tt") : null; not.status = model.Status; not.type = model.Type; long nid = 0; nservice.AddNotification(not, ref nid); } return(Json(new { message = "success" }, JsonRequestBehavior.AllowGet)); } return(Json(new { message = "error" }, JsonRequestBehavior.AllowGet)); }