Esempio n. 1
0
        public string RegisterRecipient(UserPushNotificationDevice userPushNotificationDevice)
        {
            AmazonSNS.IAmazonSimpleNotificationService client =
                Amazon.AWSClientFactory.CreateAmazonSimpleNotificationServiceClient(
                    new Amazon.Runtime.BasicAWSCredentials(Configuration.AmazonSnsAccessKey, Configuration.AmazonSnsSecretKey),
                    Amazon.RegionEndpoint.USEast1);

            // first, check if device is registered
            bool   doSnsRegistration = true;
            string currentArn        = userPushNotificationDevice.ProviderEndpointId;

            if (!String.IsNullOrEmpty(currentArn))
            { // if we have an arn, read info from Amazon
                var response = client.GetEndpointAttributes(new AmazonSNS.Model.GetEndpointAttributesRequest()
                {
                    EndpointArn = currentArn
                });
                if (response.Attributes != null && response.Attributes.ContainsKey("Enabled") && response.Attributes.ContainsKey("Token"))
                {
                    string enabledStr = response.Attributes["Enabled"];
                    bool   enabled    = false;
                    bool.TryParse(enabledStr, out enabled);

                    string token      = response.Attributes["Token"];
                    string savedToken = userPushNotificationDevice.ProviderToken;
                    bool   hasBothSavedAndSnsToken = !String.IsNullOrEmpty(token) && !String.IsNullOrEmpty(savedToken);

                    if (enabled && hasBothSavedAndSnsToken && token.Equals(savedToken))
                    {
                        doSnsRegistration = false;
                    }
                }
            }
            // then, register it if appropriate
            if (doSnsRegistration)
            {
                AmazonSNS.Model.CreatePlatformEndpointResponse response = null;
                if (userPushNotificationDevice.DeviceOS == Core.Enumerations.Messaging.DeviceOS.iOS)
                {
                    response = client.CreatePlatformEndpoint(new AmazonSNS.Model.CreatePlatformEndpointRequest()
                    {
                        Token = userPushNotificationDevice.ProviderToken,
                        PlatformApplicationArn = Configuration.AmazonSnsMobilePlatformAppArnIOS
                    });
                }
                else if (userPushNotificationDevice.DeviceOS == Core.Enumerations.Messaging.DeviceOS.Android)
                {
                    response = client.CreatePlatformEndpoint(new AmazonSNS.Model.CreatePlatformEndpointRequest()
                    {
                        Token = userPushNotificationDevice.ProviderToken,
                        PlatformApplicationArn = Configuration.AmazonSnsMobilePlatformAppArnAndroid
                    });
                }

                return(response.EndpointArn);
            }
            return(currentArn);
        }
Esempio n. 2
0
        public bool RegisterPushDevice(UserProfile user, PushDeviceRegistrationModel deviceRegistrationModel)
        {
            UserPushNotificationDevice userPushNotificationDevice = _userPushNotificationDeviceRepository.ReadUserDevice(user.UserId, deviceRegistrationModel.DeviceId, deviceRegistrationModel.DeviceOS);

            if (userPushNotificationDevice == null)
            {
                userPushNotificationDevice = new UserPushNotificationDevice()
                {
                    DeviceOS = deviceRegistrationModel.DeviceOS,
                    DeviceId = deviceRegistrationModel.DeviceId,
                    UserId   = user.UserId
                };
            }

            userPushNotificationDevice.ProviderToken      = deviceRegistrationModel.ProviderToken;
            userPushNotificationDevice.ProviderEndpointId = _pushNotificationMessageProvider.RegisterRecipient(userPushNotificationDevice);
            userPushNotificationDevice.Enabled            = true;

            _userPushNotificationDeviceRepository.CreateOrUpdate(userPushNotificationDevice);
            _uow.SaveChanges();

            // now, to create/confirm/update the application endpoint in AWS
            return(true);
        }
Esempio n. 3
0
        private void DisableDevices(List <Recipient> badRecipients)
        {
            foreach (Recipient recipient in badRecipients)
            {
                UserPushNotificationDevice device = _userPushNotificationDeviceRepository.ReadUserDevice(recipient.UserId, recipient.DeviceId, recipient.DeviceOS.Value);

                if (device != null)
                {
                    device.Enabled     = false;
                    device.ModifiedUtc = DateTime.Now.ToUniversalTime();

                    _unitOfWork.SaveChanges();

                    StringBuilder msg = new StringBuilder();

                    msg.AppendLine("Disabling device.");
                    msg.AppendLine("DeviceId: {DeviceId}");
                    msg.AppendLine("DeviceOS: {DeviceOS}");
                    msg.AppendLine("ProviderEndPoint: {ProviderEndpoint}");

                    _eventLog.WriteInformationLog(msg.ToString().Inject(recipient));
                }
            }
        }
Esempio n. 4
0
 public string RegisterRecipient(UserPushNotificationDevice userPushNotificationDevice)
 {
     throw new NotImplementedException();
 }