Пример #1
0
        public async Task <ActionResult <object> > SendMessageByToken(string token, Notification notification)
        {
            if (token == null)
            {
                return(BadRequest("Отсутствует FCM-токен для отправки уведомления"));
            }

            if (notification.Body == null || notification.Title == null)
            {
                return(BadRequest("Недостаточно данных"));
            }

            var result = await _mmc.SendAndroidNotification(token, notification.Title, notification.Body, new Dictionary <string, string>
            {
                { "Type", "Invite" },
            });

            return(Ok(result));
        }
Пример #2
0
        public async Task <ActionResult <object> > InviteFriend(string exercise, string userName)
        {
            var opponent = await _userManager.FindByNameAsync(userName);

            if (opponent == null)
            {
                return(NotFound("Оппонент не найден"));
            }

            var user = await _userManager.FindByNameAsync(User.Identity.Name);

            _context.Versus.Add(new Data.Entities.Versus
            {
                DateTime            = DateTime.Now,
                InitiatorId         = user.Id,
                InitiatorName       = user.UserName,
                Exercise            = exercise,
                Status              = "Searching",
                InitiatorIterations = 0,
                OpponentIterations  = 0,
                LastInvitedId       = opponent.Id
            });

            await _context.SaveChangesAsync();

            try
            {
                if (opponent.Online)
                {
                    var socketId = await UserToSocket(opponent.Id);

                    if (socketId != null)
                    {
                        await _messagesHandler.SendMessageAsync(socketId,
                                                                JsonSerializer.Serialize(
                                                                    new Dictionary <string, string>()
                        {
                            { "Type", "InviteFriend" },
                            { "Exercise", exercise },
                            { "UserName", user.UserName }
                        }));
                    }
                }
                else
                {
                    var result = await _mmc.SendAndroidNotification(opponent.Token, "Приглашение",
                                                                    user.UserName + " зовет вас на поединок",
                                                                    new Dictionary <string, string>
                    {
                        { "Type", "Invite" },
                        { "Exercise", exercise },
                        { "UserName", user.UserName }
                    });

                    return(Ok(result));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An errof while send invite message " + ex.Message);
                return(StatusCode(500, ex));
            }

            return(Ok());
        }