Example #1
0
        public async Task HandleAsync(RegisterFcmDeviceCommand message, CancellationToken cancellationToken)
        {
            if (!userContext.IsAuthenticated)
            {
                throw new InvalidOperationException("Cannot register FCM device for an unauthenticated user");
            }

            string             normalizedDeviceToken = message.RegistrationId;
            FcmUserDeviceToken token = await repository.FirstOrDefaultAsync <FcmUserDeviceToken>(
                x => x.RegistrationId == normalizedDeviceToken &&
                x.AppId == message.AppId);

            if (token != null)
            {
                if (token.UserId == userContext.UserId)
                {
                    return;
                }

                repository.Remove(token);
            }

            // TODO restrict AppIds (a configurable list?)

            token = new FcmUserDeviceToken(Guid.NewGuid(), await userContext.GetUserAsync(),
                                           normalizedDeviceToken, message.AppId);
            repository.Add(token);

            Logger.Debug($"Added external FCM user device registaration for user ID {userContext.UserId}");
        }
Example #2
0
        public async Task HandleAsync(DeregisterFcmDeviceCommand message, CancellationToken cancellationToken)
        {
            string             normalizedDeviceToken = message.RegistrationId;
            FcmUserDeviceToken token = await repository.FirstOrDefaultAsync <FcmUserDeviceToken>(
                x => x.RegistrationId == normalizedDeviceToken &&
                x.AppId == message.AppId);

            if (token != null)
            {
                repository.Remove(token);
            }
        }