Exemple #1
0
        public JsonResult <ApiBaseResponse> SendMessage([FromBody] ApiMessage message)
        {
            var result = new ApiBaseResponse();

            try
            {
                var messageId = ServicesHost.GetService <IMessagesProvider>().Save(message);
                message = ServicesHost.GetService <IMessagesProvider>().GetById(messageId);

                var contact      = ServicesHost.GetService <IContactsProvider>().GetById(message.ContactId);
                var receiverUser = UserManager.FindByPhoneNumber(contact.PhoneNumber);

                if (receiverUser == null)
                {
                    var smsMessageText = message.GetSmsMessageText(message);
                    ServicesHost.GetService <ISmsService>().SendSmsMessage(contact.PhoneNumber, smsMessageText);
                }
                else
                {
                    var userDeviceTokens = ServicesHost.GetService <IDevicesProvider>().GetUserDevices(receiverUser.Id).Select(d => d.Token);
                    var messagesCount    = ServicesHost.GetService <IMessagesProvider>().UnreadUserMessagesCount(receiverUser.Id);
                    var messageText      = message.GetPushNotificationText();

                    Task.Run(() => ServicesHost.GetService <IApplePushNotificationService>().SendPushNotifications(userDeviceTokens, messageText, messagesCount, messageId.ToString()));
                }
            }
            catch (Exception ex)
            {
                result.Status = ApiStatusCode.SystemError;
                result.Error  = ex.Message;
            }

            return(Json(result));
        }