Ejemplo n.º 1
0
        public async Task AddTokenToDb(string tokenValue)
        {
            using (var _db = new MoMoFirebaseDbContext())
            {
                var existingToken = await _db.FCMTokens.FirstOrDefaultAsync(t => t.FcmTokenValue.Equals(tokenValue));

                if (existingToken == null)
                {
                    FCMToken newData = new FCMToken()
                    {
                        FcmTokenValue = tokenValue,
                        Active        = true,
                        CreatedUtc    = DateTime.UtcNow,
                        DeviceType    = "WebBrowser"
                    };

                    _db.FCMTokens.Add(newData);
                }
                else
                {
                    existingToken.ModifiedUtc = DateTime.UtcNow;
                    existingToken.Active      = true;
                }

                await _db.SaveChangesAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task <SendNotificationResponse> SendNotificationToAllUsersAsync(MessageData messageData)
        {
            SendNotificationResponse response;

            using (var _db = new MoMoFirebaseDbContext())
            {
                var fcmTokens = await _db.FCMTokens.Where(t => t.Active == true)
                                .Select(t => t.FcmTokenValue)
                                .ToListAsync();

                if (fcmTokens != null && fcmTokens.Count > 0)
                {
                    var postData = new PostData()
                    {
                        MessageData = messageData,
                        FcmTokens   = fcmTokens
                    };

                    response = await SendNotificationAsync(postData);
                }
                else
                {
                    response = new SendNotificationResponse()
                    {
                        SendNotificationStatus = SendNotificationEnum.MissingToken,
                        StatusMessage          = "Don't have active tokens in database!"
                    };
                }

                return(response);
            }
        }
Ejemplo n.º 3
0
        private async Task RemoveRegistrationAsync(string[] fcmTokens)
        {
            if (fcmTokens?.Any() == true)
            {
                using (var _db = new MoMoFirebaseDbContext())
                {
                    var fcmTokensDb = await _db.FCMTokens
                                      .Where(b => fcmTokens.Contains(b.FcmTokenValue) && b.Active == true)
                                      .ToListAsync();

                    if (fcmTokensDb.Count > 0)
                    {
                        foreach (var fcmToken in fcmTokensDb)
                        {
                            fcmToken.Active      = false;
                            fcmToken.ModifiedUtc = DateTime.UtcNow;
                        }
                        await _db.SaveChangesAsync();
                    }
                }
            }
        }