Пример #1
0
        public IActionResult DoneTask(int id)
        {
            var task = _employeeTaskRepository.GetTaskById(id);

            if (task != null)
            {
                task.isDone = true;
                var noti = new AdminNotifications
                {
                    CreatedDate = DateTime.Now,
                    FromUserId  = _taskForEmployeeRepository.GetTaskById(id).EmployeeId,
                    IsRead      = false,
                    NotiBody    = "Employee with id " + _taskForEmployeeRepository.GetTaskById(id).EmployeeId
                                  + "finished task with id " + id,
                    NotiHeader = "Finished Task #" + id,
                    ToRole     = ERole.Admin
                };
                _notificationRepository.AddNotificationsForAdmin(noti);
            }
            else
            {
                _logger.LogWarning("Task with id " + id + "doesn't exist");
            }
            return(RedirectToAction("Success", "Success"));
        }
        public async Task <IActionResult> Summary(SummaryViewModel summary, bool isOkay)
        {
            if (ModelState.IsValid && isOkay)
            {
                var model = _mapper.MapSummaryViewModelToFormModel(summary);
                model.CreatedDate     = DateTime.Now;
                model.ApplicationUser = await _userManager.FindByIdAsync(summary.UserId);

                var empTask = _mapper.MapSummaryViewModelToEmployeeTask(summary);
                empTask.isDone = false;

                var clientNoti = new ClientNotifications
                {
                    CreatedDate = DateTime.Now,
                    FromRole    = ERole.Admin,
                    ToClientId  = summary.UserId,
                    IsRead      = false,
                    NotiBody    = "Hello your form is sent.",
                    NotiHeader  = "Form confirmation"
                };
                var adminNoti = new AdminNotifications
                {
                    CreatedDate = DateTime.Now,
                    FromUserId  = summary.UserId,
                    ToRole      = ERole.Admin,
                    IsRead      = false,
                    NotiBody    = "Client with id " + summary.UserId + "sent form. ",
                    NotiHeader  = "New form from Client " + summary.Company
                };
                try
                {
                    _notificationRepository.AddNotificationsForClient(clientNoti);
                    _notificationRepository.AddNotificationsForAdmin(adminNoti);
                    _employeeTaskRepository.AddEmployeeTask(empTask);
                    _formRepository.AddTask(model);
                }
                catch (Exception e)
                {
                    _logger.LogError("Error while adding to database", e);
                    Console.WriteLine(e.Message); // add alert in view
                }
                return(RedirectToAction("Success", "Success"));
            }
            else
            {
                _logger.LogWarning("Invalid modelstate in summary");
            }
            return(View(summary));
        }
Пример #3
0
        public async Task SendAndroidPushNotification(List <UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int Type = 0)
        {
            try
            {
                if (devices.Count() == 0)//it means their is no device no need to run the below code
                {
                    return;
                }

                NotificationModel msgModel = new NotificationModel();

                foreach (var device in devices.Where(x => x.IsActive))
                {
                    GcmServiceBroker gcmBroker;

                    if (AdminNotification != null)
                    {
                        // msgModel.Type = (int)PushNotificationType.Announcement;
                        var notification = device.User.Notifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id);
                        msgModel.Message        = AdminNotification.Description;
                        msgModel.NotificationId = notification.Id;
                        msgModel.Title          = notification.Title;
                    }
                    else if (OtherNotification != null)
                    {
                        //msgModel.Type = Type;
                        msgModel.Message        = OtherNotification.Text;
                        msgModel.NotificationId = OtherNotification.Id;
                        msgModel.Title          = OtherNotification.Title;
                    }

                    if (device.ApplicationType == UserDevice.ApplicationTypes.Enterprise)
                    {
                        gcmBroker = new GcmServiceBroker(Enterprise.Android.AndroidGCMConfig);
                    }
                    else
                    {
                        gcmBroker = new GcmServiceBroker(PlayStore.Android.AndroidGCMConfig);
                    }

                    gcmBroker.OnNotificationFailed    += AndroidNotificationFailed;
                    gcmBroker.OnNotificationSucceeded += AndroidNotificationSuccess;
                    gcmBroker.Start();

                    gcmBroker.QueueNotification(
                        new GcmNotification
                    {
                        RegistrationIds = new List <string> {
                            device.AuthToken
                        },
                        Priority = GcmNotificationPriority.High,
                        Data     = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(msgModel))
                    });

                    gcmBroker.Stop();
                    gcmBroker.OnNotificationFailed    -= AndroidNotificationFailed;
                    gcmBroker.OnNotificationSucceeded -= AndroidNotificationSuccess;
                }
            }
            catch (Exception ex)
            {
                Utility.LogError(ex);
            }
        }
Пример #4
0
        //public void SendIOSPushNotification(List<UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int EntityType = 0, int EntityId = 0)
        //{
        //    string serverKey = GCMWebAPIKey;
        //    var result = "-1";
        //    var notificationid = 0;

        //    try
        //    {
        //        var webAddr = "https://fcm.googleapis.com/fcm/send";

        //        foreach (var device in devices.Where(x => x.IsActive))
        //        {
        //            var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
        //            httpWebRequest.ContentType = "application/json";
        //            httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey);
        //            httpWebRequest.Method = "POST";
        //            NotificationMessage pushModel = new NotificationMessage();
        //            if (AdminNotification != null)
        //            {

        //                pushModel.notification.title = AdminNotification.Title;
        //                pushModel.notification.text= AdminNotification.Description;


        //                //pushModel.data.contentavailable = 1;
        //            }
        //            else
        //            {
        //                pushModel.notification.title = OtherNotification.Title;
        //                pushModel.notification.text = OtherNotification.Text;
        //                //pushModel.notification.NotificationId = OtherNotification.Id;
        //                //pushModel.notification.DeliveryMan_Id = OtherNotification.DeliveryMan_ID;
        //               // pushModel.notification.EntityType = EntityType;
        //               // pushModel.notification.EntityId = OtherNotification.EntityId;
        //               //pushModel.aps.contentavailable = 1;
        //            }

        //            using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        //            {
        //                //var dta = new DynamicValuesModel
        //                //{
        //                //    entityid = pushModel.notification.EntityId,
        //                //    entitytype = pushModel.notification.EntityType,
        //                //    notificationid = pushModel.notification.NotificationId,
        //                //    isread = true
        //                //};



        //                var messageInformation = new NotificationMessage();
        //                //string[] authTokens = new string[1];
        //                //authTokens[0]=new string
        //                string[] authTokens = { device.AuthToken };


        //                messageInformation.registration_ids = authTokens;

        //               // messageInformation.data = dta;
        //                //Object to JSON STRUCTURE => using Newtonsoft.Json;
        //                string jsonMessage = JsonConvert.SerializeObject(messageInformation);
        //                streamWriter.Write(jsonMessage);
        //                streamWriter.Flush();
        //            }

        //            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        //            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        //            {
        //                result = streamReader.ReadToEnd();
        //                if (result.Contains("success") && result.Contains("failure"))
        //                {
        //                    dynamic token = JObject.Parse(result);
        //                    string success = token.success.ToString();
        //                    //return success == "1" ? true : false;
        //                }
        //                else
        //                {
        //                }
        //            }
        //        }

        //        // return result;
        //    }
        //    catch (Exception ex)
        //    {
        //        Utility.LogError(ex);
        //    }

        //}



        public void SendIOSPushNotification(List <UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int EntityType = 0, int EntityId = 0)
        {
            string serverKey      = GCMWebAPIKey;
            var    result         = "-1";
            var    notificationid = 0;

            try
            {
                var webAddr = "https://fcm.googleapis.com/fcm/send";

                foreach (var device in devices.Where(x => x.IsActive))
                {
                    var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                    httpWebRequest.ContentType = "application/json";
                    httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey);
                    httpWebRequest.Method = "POST";
                    IosPush pushModel = new IosPush();
                    if (AdminNotification != null)
                    {
                        pushModel.aps.alert.title = AdminNotification.Title;
                        pushModel.aps.alert.body  = AdminNotification.Description;
                        //using (RiscoContext ctx=new RiscoContext())
                        //{
                        //    ctx.Notifications.Count();
                        //}

                        //if (device.User != null)
                        //    pushModel.notification.NotificationId = device.User.SendingUserNotifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id;
                        //else
                        //pushModel.notification.NotificationId = device.DeliveryMan.Notifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id;
                        pushModel.notification.EntityType = (int)PushNotificationType.Announcement;
                        //pushModel.notification.EntityId = OtherNotification.Entity_ID.Value;
                        pushModel.aps.contentavailable = 1;
                    }
                    else
                    {
                        pushModel.aps.alert.title             = OtherNotification.Title;
                        pushModel.aps.alert.body              = OtherNotification.Text;
                        pushModel.notification.NotificationId = OtherNotification.Id;
                        //pushModel.notification.DeliveryMan_Id = OtherNotification.DeliveryMan_ID;
                        pushModel.notification.EntityType = EntityType;
                        pushModel.notification.EntityId   = 1;
                        pushModel.aps.contentavailable    = 1;
                    }

                    using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                    {
                        var dta = new DynamicValuesModel
                        {
                            entityid       = pushModel.notification.EntityId,
                            entitytype     = pushModel.notification.EntityType,
                            notificationid = pushModel.notification.NotificationId,
                            isread         = true
                        };



                        var messageInformation = new NotificationMessage();
                        //string[] authTokens = new string[1];
                        //authTokens[0]=new string
                        string[] authTokens = { device.AuthToken };

                        messageInformation.notification = new SendNotification()
                        {
                            title = pushModel.aps.alert.title,
                            text  = pushModel.aps.alert.body
                        };
                        messageInformation.registration_ids = authTokens;

                        messageInformation.data = dta;
                        //Object to JSON STRUCTURE => using Newtonsoft.Json;
                        string jsonMessage = JsonConvert.SerializeObject(messageInformation);


                        //string json = "";
                        //json = "{\"to\": \"" + device.AuthToken + "\",\"notification\": {\"body\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + pushModel.notification.NotificationId + "\",\"entitytype\": \"" + pushModel.notification.EntityType + "\",\"entityid\": \"" + pushModel.notification.EntityId + "\",\"isread\": \"" + true + "\",}}";
                        //json = "{\"to\": \"" + device.AuthToken + "\",\"notification\": {\"text\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + notificationid + "\",\"isread\": \"" + true + "\",},\"data\":{\"entitytype\": \"" + pushModel.notification.EntityType + "\",\"entityid\": \"" + pushModel.notification.EntityId + "\",}}";
                        streamWriter.Write(jsonMessage);
                        streamWriter.Flush();
                    }

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        result = streamReader.ReadToEnd();
                        if (result.Contains("success") && result.Contains("failure"))
                        {
                            dynamic token   = JObject.Parse(result);
                            string  success = token.success.ToString();
                            //return success == "1" ? true : false;
                        }
                        else
                        {
                        }
                    }
                }

                // return result;
            }
            catch (Exception ex)
            {
            }
        }
Пример #5
0
 public void AddNotificationsForAdmin(AdminNotifications notifi)
 {
     _context.AdminNotifications.Add(notifi);
     _context.SaveChanges();
 }
Пример #6
0
        public void SendIOSPushNotification(List <UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int Type = 0)
        {
            try
            {
                // Configuration (NOTE: .pfx can also be used here)
                if (devices.Count() == 0) //it means there is no device no need to run the below code
                {
                    return;
                }

                if (ApnsConfig != null)
                {
                    IosPush pushModel = new IosPush();

                    foreach (var device in devices.Where(x => x.IsActive))
                    {
                        try
                        {
                            if (AdminNotification != null)
                            {
                                pushModel.aps.alert.title             = AdminNotification.Title;
                                pushModel.aps.alert.body              = AdminNotification.Description;
                                pushModel.notification.NotificationId = device.User.Notifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id;
                                pushModel.notification.Type           = (int)PushNotificationType.Announcement;
                            }
                            else
                            {
                                pushModel.aps.alert.title             = OtherNotification.Title;
                                pushModel.aps.alert.body              = OtherNotification.Text;
                                pushModel.notification.NotificationId = OtherNotification.Id;
                                pushModel.notification.Type           = Type;
                            }

                            ApnsServiceBroker apnsBroker;

                            if (device.ApplicationType == UserDevice.ApplicationTypes.Enterprise)
                            {
                                if (device.EnvironmentType == UserDevice.ApnsEnvironmentTypes.Production)
                                {
                                    apnsBroker = new ApnsServiceBroker(Enterprise.IOS.ProductionConfig);
                                }
                                else // Sandbox/Development
                                {
                                    apnsBroker = new ApnsServiceBroker(Enterprise.IOS.SandboxConfig);
                                }
                            }
                            else //PlayStore
                            {
                                if (device.EnvironmentType == UserDevice.ApnsEnvironmentTypes.Production)
                                {
                                    apnsBroker = new ApnsServiceBroker(PlayStore.IOS.ProductionConfig);
                                }
                                else // Sandbox/Development
                                {
                                    apnsBroker = new ApnsServiceBroker(device.iOSPushConfiguration = PlayStore.IOS.SandboxConfig);
                                }
                            }

                            apnsBroker.OnNotificationFailed    += IOSPushNotificationFailed;
                            apnsBroker.OnNotificationSucceeded += IOSNotificationSuccess;

                            // Start the broker
                            apnsBroker.Start();
                            apnsBroker.QueueNotification(new ApnsNotification
                            {
                                DeviceToken = device.AuthToken,
                                Payload     = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(pushModel))
                            });

                            apnsBroker.Stop();
                            apnsBroker.OnNotificationFailed    -= IOSPushNotificationFailed;
                            apnsBroker.OnNotificationSucceeded -= IOSNotificationSuccess;
                        }
                        catch (Exception ex)
                        {
                            Utility.LogError(ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.LogError(ex);
            }
        }
Пример #7
0
 public NotificationViewModel MapAdminNotificationsToNotificationViewModel(AdminNotifications adminNotifications)
 {
     return(_mapper.Map <NotificationViewModel>(adminNotifications));
 }