Example #1
0
        private bool RegisterMobile(int userId, string username, MobileInfo currentMobile)
        {
            if (currentMobile.Os == MobileOS.iOS && string.IsNullOrEmpty(currentMobile.Token))
            {
                _accountStatus = UserAccountMessages.MissingDeviceToken;
                return(false);
            }

            if (currentMobile.Os == MobileOS.Android && string.IsNullOrEmpty(currentMobile.InstallationId))
            {
                _accountStatus = UserAccountMessages.MissingInstallationId;
                return(false);
            }

            if (currentMobile.Os == MobileOS.Android)
            {
                return(true);
            }

            var mobile = new UserMobileDevice
            {
                UserId         = userId,
                Active         = true,
                Token          = currentMobile.Token,
                InstallationId = currentMobile.InstallationId,
                OS             = currentMobile.Os.ToString(),
                Version        = currentMobile.Version,
                Device         = currentMobile.Device,
                Created        = UtcNow
            };

            Console.Write($"Subscribe to a push notification service {username}");
            //TODO: Subscribe to a push notification service
            //_parseService.SubscribeAsync(mobile, username);
            return(true);
        }
Example #2
0
        public string RegisterDevice(string token, string deviceType, string deviceId)
        {
            UnitOfWork unitOfWork = new UnitOfWork(_sessionHelper.GetSessionFactory("APL"));

            Repository<Guid, User> userRepo = new Repository<Guid, User>(unitOfWork.Session);

            JavaScriptSerializer js = new JavaScriptSerializer();
            JsonSerializerSettings j = new JsonSerializerSettings();
            j.TypeNameHandling = TypeNameHandling.Objects;
            j.Formatting = Newtonsoft.Json.Formatting.Indented;

            string responseString = null;
            Response response = new Response();

            user = CheckToken(token);
            if (user.AuthToken == null || user.AuthToken != token)
            {
                user.AuthToken = token;
                userRepo.Update(user);
            }

            if (!string.IsNullOrEmpty(user.AuthToken))
            {
                if (CheckToken(user.AuthToken) == null)
                {
                    user.AuthToken = null;
                }
            }

            if (user != null)
            {
                if (DEVICE_TYPES.Contains(deviceType.ToLower()))
                {
                    UserMobileDevice userMobileDevice = new UserMobileDevice();
                    Repository<Guid, UserMobileDevice> userMobileDevRepo = new Repository<Guid, UserMobileDevice>(unitOfWork.Session);
                    userMobileDevice = userMobileDevRepo.FilterBy(x => x.Token == deviceId).FirstOrDefault();
                    if (userMobileDevice != null && userMobileDevice.Id != Guid.Empty)
                    {
                        if (userMobileDevice.MobileDeviceType.ToLower() == deviceType.ToLower() && userMobileDevice.User.Id == user.Id)
                        {
                            response.ResponseCode = "OK";
                            response.ResponseData = "Device registered.";
                        }
                        else
                        {
                            response.ResponseCode = "ERR";
                            response.ResponseData = "Invalid device registration";
                        }

                    }
                    else
                    {
                        userMobileDevice = new UserMobileDevice();
                        userMobileDevice.MobileDeviceType = deviceType.ToLower();
                        userMobileDevice.Token = deviceId;
                        userMobileDevice.User = user;
                        userMobileDevice.EnablePushNotifications = true;
                        user.MobileDevices.Add(userMobileDevice);
                        userMobileDevRepo.Add(userMobileDevice);
                        response.ResponseCode = "OK";
                        response.ResponseData = "Device registered.";
                    }
                }
                else
                {
                    response.ResponseCode = "ERR";
                    response.ResponseData = "Unknown DeviceType";
                }

            }
            else
            {
                response.ResponseCode = "ERR";
                response.ResponseData = "Authentication required.";
            }
            unitOfWork.Commit();

            js = new JavaScriptSerializer();
            responseString = JsonConvert.SerializeObject(response, j);
            return responseString;
        }