public async Task <IActionResult> SaveMatch([FromBody] Match model)
        {
            try
            {
                if (_db.Matches.Where(x => x.OpportunityId == model.OpportunityId && x.UserId == model.UserId).Count() == 0)
                {
                    model.CreationDate = DateTime.Now;
                    model.LastUpdate   = DateTime.Now;

                    var Sender = await _db.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

                    var opp = await _db.Opportunities.Include(x => x.KPIs).Include(u => u.User).FirstOrDefaultAsync(x => x.Id == model.OpportunityId);

                    var Receiver = await _db.Users.FindAsync(opp.User.Id);

                    var AmIBlocked = await _db.BlockedUsers.FirstOrDefaultAsync(x => !x.IsDeleted && x.UserId == Receiver.Id && x.BlockedUserId == Sender.Id);

                    if (AmIBlocked != null)
                    {
                        return(BadRequest("You cant send a match to " + Receiver.Name + " since you are blocked."));
                    }

                    if (Sender != null && opp != null)
                    {
                        _db.Matches.Add(model);
                        Sender.Naos              += 100;
                        Receiver.Naos            += 100;
                        _db.Entry(Sender).State   = EntityState.Modified;
                        _db.Entry(Receiver).State = EntityState.Modified;
                        await _db.SaveChangesAsync();

                        await Task.Run(async() => {
                            FCMClient client = new FCMClient(ServerApiKey); //as derived from https://console.firebase.google.com/project/
                            var message      = new FirebaseNet.Messaging.Message()
                            {
                                To           = Receiver.FireBaseToken, //topic example /topics/all
                                Notification = new IOSNotification()
                                {
                                    Body  = "The user " + Sender.Name + " sent you a match.",
                                    Title = opp.Title + " Match!!",
                                },
                            };
                            var result = await client.SendMessageAsync(message);
                        });

                        return(StatusCode(StatusCodes.Status200OK));
                    }
                    return(BadRequest(ModelState));
                }
                else
                {
                    return(BadRequest("You’ve already sent a match to this opportunity and user."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
        public async Task <IActionResult> MatchBack([FromBody] Match model)
        {
            try
            {
                if (_db.Matches.Where(x => x.OpportunityId == model.OpportunityId && x.UserId == model.UserId).Count() == 1)
                {
                    var Receiver = await _db.Users.FirstOrDefaultAsync(x => x.Id == model.UserId);

                    var opp = await _db.Opportunities.Include(x => x.KPIs).Include(u => u.User).FirstOrDefaultAsync(x => x.Id == model.OpportunityId);

                    var Sender = await _db.Users.FindAsync(opp.User.Id);

                    var Match = await _db.Matches.FindAsync(model.Id);

                    if (Sender != null && opp != null && Receiver != null)
                    {
                        Match.LastUpdate = DateTime.Now;
                        Match.Status     = Status.Matched;
                        _db.Matches.Update(Match);


                        Sender.Naos   += 150;
                        Receiver.Naos += 150;

                        _db.Entry(Sender).State   = EntityState.Modified;
                        _db.Entry(Receiver).State = EntityState.Modified;


                        await _db.SaveChangesAsync();

                        await Task.Run(async() => {
                            FCMClient client = new FCMClient(ServerApiKey); //as derived from https://console.firebase.google.com/project/
                            var message      = new FirebaseNet.Messaging.Message()
                            {
                                To           = Receiver.FireBaseToken, //topic example /topics/all
                                Notification = new IOSNotification()
                                {
                                    Body  = "The user " + Sender.Name + " matched you back.",
                                    Title = opp.Title + " Matched back!",
                                },
                            };
                            var result = await client.SendMessageAsync(message);
                        });

                        return(StatusCode(StatusCodes.Status200OK));
                    }
                    return(BadRequest(ModelState));
                }
                else
                {
                    return(BadRequest("You already matched back this opportunity and user."));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
        public static async Task SendMessage(User recipient, IDictionary <string, string> data = null)
        {
            var client = new FCMClient(FcmServerKey);

            var message = new Message()
            {
                To   = recipient.FirebaseToken,
                Data = data
            };

            await client.SendMessageAsync(message);
        }
        public async Task <IActionResult> SendMessage([FromBody] Entities.Message sentMessage)
        {
            try
            {
                var Receiver = await _db.Users.FindAsync(sentMessage.ReceiverId);

                var Sender = await _db.Users.FindAsync(sentMessage.SenderId);

                var AmIBlocked = await _db.BlockedUsers.FirstOrDefaultAsync(x => !x.IsDeleted && x.UserId == Receiver.Id && x.BlockedUserId == Sender.Id);

                if (AmIBlocked != null)
                {
                    return(BadRequest("You cant send a message to " + Receiver.Name + " since you are blocked."));
                }

                if (sentMessage != null)
                {
                    _db.Message.Add(sentMessage);
                    await _db.SaveChangesAsync();

                    await Task.Run(async() => {
                        FCMClient client = new FCMClient(ServerApiKey); //as derived from https://console.firebase.google.com/project/

                        var message = new FirebaseNet.Messaging.Message()
                        {
                            To           = Receiver.FireBaseToken, //topic example /topics/all
                            Notification = new IOSNotification()
                            {
                                Body  = sentMessage.Text,
                                Title = Sender.Name,
                            },
                            Data = new Dictionary <string, string>
                            {
                                { "Body", sentMessage.Text },
                                { "Title", "New Message" },
                                { "SenderId", sentMessage.SenderId },
                                { "SenderPicture", Sender?.ProfilePicUrl }
                            }
                        };
                        var result = await client.SendMessageAsync(message);
                    });

                    return(Ok());
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
 private static async Task SendFcmMessage(string pushName, string body, NotificationUser user, string token)
 {
     FCMClient client  = new FCMClient(GetServerKey());
     var       message = new FirebaseNet.Messaging.Message()
     {
         To   = token,
         Data = new Dictionary <string, string>
         {
             { "badge", (user.UnreadNotificationCount + 1).ToString() },
             { "app-action", @"new-message" },
             { "message", body }
         }
     };
     var response = client.SendMessageAsync(message);
     await response.ContinueWith(t =>
     {
         var results = t.Result as DownstreamMessageResponse;
         _logQueue.Enqueue(@" ------------" + DateTime.Now + "--------------------" + '\n' + "sent to  " + user.AppUserId + " is success : " + results.Success + '\n' + "errors : " + String.Join(" , ", results.Results.Select(x => x.Error)));
     });
 }
        private static int SendFcmBatchMessages(string body, List <NotificationUser> users) // restricted to 1000
        {
            FCMClient client  = new FCMClient(GetServerKey());
            var       message = new FirebaseNet.Messaging.Message()
            {
                RegistrationIds = users.Select(x => x.FcmToken).ToList(),
                Data            = new Dictionary <string, string>
                {
                    { "app-action", @"new-message" },
                    { "message", body }
                }
            };
            var res         = new List <DownstreamMessageResponse>();
            var response    = client.SendMessageAsync(message);
            var contiuation = response.ContinueWith(t => res.Add((DownstreamMessageResponse)t.Result));

            Notification.UpdateUnreadNotificationCountOfUsers(users.Select(x => x.AppUserId).ToList());
            contiuation.Wait();
            return((int)res.Sum(x => x.Success));
        }
        public static async Task SendMessage(User recipient, string title, string body,
                                             IDictionary <string, string> data = null, string clickAction = null, string sound = "default",
                                             string icon = "default")
        {
            var client = new FCMClient(FcmServerKey);

            var message = new Message()
            {
                To           = recipient.FirebaseToken,
                Notification = new AndroidNotification()
                {
                    Body        = body,
                    Title       = title,
                    Icon        = icon,
                    ClickAction = clickAction,
                    Sound       = sound,
                },
                Data = data
            };

            await client.SendMessageAsync(message);
        }