public async Task <IActionResult> SendPushNotificationToApp([FromBody] AdminNotificationBindingModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }
                var response = await _bONotification.SendPushNotificationToApp(model);

                switch (response)
                {
                case HttpStatusCode.OK:
                    return(Ok(new CustomResponse <Error> {
                        Message = Global.ResponseMessages.Success, StatusCode = (int)HttpStatusCode.OK, Result = new Error {
                            ErrorMessage = "Notification sent successfully!"
                        }
                    }));

                default:
                    return(Ok(new CustomResponse <Error> {
                        Message = Global.ResponseMessages.Conflict, StatusCode = (int)HttpStatusCode.Conflict, Result = new Error {
                            ErrorMessage = "Failure!"
                        }
                    }));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(Error.LogError(ex)));
            }
        }
Example #2
0
        public async Task <object> SendPushNotificationToApp(AdminNotificationBindingModel model)
        {
            try
            {
                UserTypes userType        = UserTypes.User;
                var       targetedDevices = _dbContext.UserDevices.Where(x => x.IsDeleted == false).ToList();
                if (model.TargetAudienceType == TargetAudienceType.User)
                {
                    targetedDevices = targetedDevices.Where(x => x.User_Id != null).ToList();
                }
                else if (model.TargetAudienceType == TargetAudienceType.Driver)
                {
                    targetedDevices = targetedDevices.Where(x => x.Driver_Id != null).ToList();
                    userType        = UserTypes.Driver;
                }

                if (model.CityId > 0)
                {
                    List <int> Ids = _dbContext.Users.Where(x => x.IsDeleted == false && x.City_Id == model.CityId).Select(x => x.Id).ToList();
                }
                NotificationModel Notification = new NotificationModel();
                Notification.Title = model.Text;
                Notification.Text  = model.Description;
                Notification.PushNotificationType = PushNotificationType.Announcement;
                List <string> usersToPushIOS     = new List <string>();
                List <string> usersToPushAndriod = new List <string>();
                usersToPushAndriod = targetedDevices.Where(x => x.Platform == true && x.IsActive == true).Select(a => a.AuthToken).ToList();
                usersToPushIOS     = targetedDevices.Where(x => x.Platform == false && x.IsActive == true).Select(a => a.AuthToken).ToList();
                PushNotificationsHelper.SendIOSPushNotifications(Message: model.Description, Title: model.Title, DeviceTokens: targetedDevices.Where(x1 => x1.Platform == false & x1.IsActive == true).Select(a => a.AuthToken).ToList(), userType: userType);
                PushNotificationsHelper.SendAndroidPushNotifications(Message: model.Description, Title: model.Title, DeviceTokens: targetedDevices.Where(x => x.Platform == true && x.IsActive == true).Select(a => a.AuthToken).ToList(), userType: userType);
                return(HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }