Beispiel #1
0
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            CreatePlatformEndpointResponse response = new CreatePlatformEndpointResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("CreatePlatformEndpointResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
Beispiel #2
0
        private static void UnmarshallResult(XmlUnmarshallerContext context, CreatePlatformEndpointResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("EndpointArn", targetDepth))
                    {
                        var unmarshaller = StringUnmarshaller.Instance;
                        response.EndpointArn = unmarshaller.Unmarshall(context);
                        continue;
                    }
                }
            }

            return;
        }
Beispiel #3
0
        private async Task <DynamoSubscriber> CreateSubscriberAsync(DynamoSubscriber prevSubscriber)
        {
            string endpointArn = null;

            try
            {
                CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest();
                request.PlatformApplicationArn = model.ApplicationPlatformArn;
                request.Token = model.Token;
                CreatePlatformEndpointResponse response = await snsClient.CreatePlatformEndpointAsync(request);

                endpointArn = response.EndpointArn;
            }
            catch (InvalidParameterException ex)
            {
                Console.WriteLine("InvalidParameterException Message = " + ex.Message);

                var match = existingEndpointRegex.Match(ex.Message);
                if (match.Success)
                {
                    // The endpoint already exists for this token, but with additional custom data that
                    // CreateEndpoint doesn't want to overwrite. Just use the existing endpoint.
                    endpointArn = match.Groups[1].Value;
                }
                else
                {
                    throw ex;
                }
            }

            DynamoSubscriber newSubscriber = await CreateDynamoSubscriber(endpointArn);

            if (prevSubscriber != null)
            {
                if (prevSubscriber.EndpointARN != newSubscriber.EndpointARN)
                {
                    await DeleteEndpointAsync(prevSubscriber.EndpointARN);
                }
            }

            if (tagOperator.IsTaggingAvailable())
            {
                await ReAssignTagsForUserWithNewSubscriber(newSubscriber);
            }

            return(newSubscriber);
        }
        private void RegisterDevice()
        {
#if UNITY_ANDROID
            if (string.IsNullOrEmpty(GoogleConsoleProjectId))
            {
                Debug.Log("sender id is null");
                return;
            }
            GCM.Register((regId) =>
            {
                if (string.IsNullOrEmpty(regId))
                {
                    ResultText.text = string.Format("Failed to get the registration id");
                    return;
                }

                ResultText.text = string.Format(@"Your registration Id is = {0}", regId);

                SnsClient.CreatePlatformEndpointAsync(
                    new CreatePlatformEndpointRequest
                {
                    Token = regId,
                    PlatformApplicationArn = AndroidPlatformApplicationArn
                },
                    (resultObject) =>
                {
                    if (resultObject.Exception == null)
                    {
                        CreatePlatformEndpointResponse response = resultObject.Response;
                        _endpointArn     = response.EndpointArn;
                        ResultText.text += string.Format(@"Platform endpoint arn is = {0}", response.EndpointArn);
                    }
                }
                    );
            }, GoogleConsoleProjectId);
#elif UNITY_IOS
#if UNITY_5
            UnityEngine.iOS.NotificationServices.RegisterForNotifications(UnityEngine.iOS.NotificationType.Alert | UnityEngine.iOS.NotificationType.Badge | UnityEngine.iOS.NotificationType.Sound);
#else
            NotificationServices.RegisterForRemoteNotificationTypes(RemoteNotificationType.Alert | RemoteNotificationType.Badge | RemoteNotificationType.Sound);
#endif
            CancelInvoke("CheckForDeviceToken");
            InvokeRepeating("CheckForDeviceToken", 1f, 1f);
#endif
        }
        private void CheckForDeviceToken()
        {
#if UNITY_IOS
#if UNITY_5
            var token = UnityEngine.iOS.NotificationServices.deviceToken;
            var error = UnityEngine.iOS.NotificationServices.registrationError;
#else
            var token = NotificationServices.deviceToken;
            var error = NotificationServices.registrationError;
#endif
            if (count >= 10 || !string.IsNullOrEmpty(error))
            {
                CancelInvoke("CheckForDeviceToken");
                Debug.Log(@"Cancel polling");
                return;
            }

            if (token != null)
            {
                deviceToken = System.BitConverter.ToString(token).Replace("-", "");
                Debug.Log("device token  = " + deviceToken);
                ResultText.text = string.Format(@"Your device token is = {0}", deviceToken);
                SnsClient.CreatePlatformEndpointAsync(
                    new CreatePlatformEndpointRequest
                {
                    Token = deviceToken,
                    PlatformApplicationArn = iOSPlatformApplicationArn
                },
                    (resultObject) =>
                {
                    if (resultObject.Exception == null)
                    {
                        CreatePlatformEndpointResponse response = resultObject.Response;
                        _endpointArn     = response.EndpointArn;
                        ResultText.text += string.Format("\n Subscribed to Platform endpoint arn = {0}", response.EndpointArn);
                    }
                }
                    );

                CancelInvoke("CheckForDeviceToken");
            }
            count++;
#endif
        }
Beispiel #6
0
        /// <summary>
        /// Registra um dispositivo no Aws SNS e retorna o ARN para posteriores notificações
        /// </summary>
        /// <param name="device"></param>
        /// <returns></returns>
        public static string RegisterNotificationEndpoint(DeviceEntity device)
        {
            string platFormArn = "";

            switch (device.DeviceType)
            {
            case DeviceType.WINDOWS_PHONE:
                platFormArn = WNSDispatcher.SNS_APP_ARN;
                break;

            case DeviceType.IOS:
                platFormArn = APNSDispatcher.SNS_APP_ARN;
                break;

            case DeviceType.ANDROID:
                platFormArn = GCMDispatcher.SNS_APP_ARN;
                break;

            default:
                platFormArn = "";
                break;
            }

            // Find in aws exception
            string existingEndpointRegexString = "Reason: Endpoint (.+) already exists with the same Token";
            Regex  existingEndpointRegex       = new Regex(existingEndpointRegexString);

            CreatePlatformEndpointRequest request = new CreatePlatformEndpointRequest()
            {
                PlatformApplicationArn = platFormArn,
                Token          = device.Endpoint,
                CustomUserData = device.UserId + ""
            };

            CreatePlatformEndpointResponse result = null;

            try
            {
                result = AWSFactory.SNSClient.CreatePlatformEndpoint(request);
            }
            catch (AmazonSimpleNotificationServiceException e)
            {
                if (e.ErrorCode == "InvalidParameter")
                {
                    var match = existingEndpointRegex.Match(e.Message);
                    if (match.Success)
                    {
                        string arn = match.Groups[1].Value;
                        AWSFactory.SNSClient.DeleteEndpoint(new DeleteEndpointRequest
                        {
                            EndpointArn = arn,
                        });

                        result = AWSFactory.SNSClient.CreatePlatformEndpoint(request);
                    }
                }
            }

            if (result != null && result.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                return(result.EndpointArn);
            }
            return(null);
        }
        public static async Task RegisterDevice(Platform platform, string registrationId)
        {
            var arn          = string.Empty;
            var _endpointArn = string.Empty;

            switch (platform)
            {
            case Platform.Android:
                arn = Constants.AndroidPlatformApplicationArn;
                break;

            case Platform.IOS:
                arn = Constants.iOSPlatformApplicationArn;
                break;
            }
            var response = new CreatePlatformEndpointResponse();
            var userData = "";

            try
            {
                if (!string.IsNullOrWhiteSpace(Settings.Uid))
                {
                    // Add Your User Data Here
                    //userData = Settings.Uid;
                }

                if (string.IsNullOrEmpty(Settings.Arnsns))
                {
                    response = await SnsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest
                    {
                        Token = registrationId,
                        PlatformApplicationArn = arn,
                        CustomUserData         = userData
                    });

                    Settings.Arnsns = response.EndpointArn;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Error: " + ex);
            }

            if (!string.IsNullOrWhiteSpace(Settings.Arnsns))
            {
                var results = new GetEndpointAttributesResponse();

                try
                {
                    var test = new GetEndpointAttributesRequest();
                    test.EndpointArn = Settings.Arnsns;


                    results = await SnsClient.GetEndpointAttributesAsync(test);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error" + ex);
                }


                if (results.Attributes == null || results.Attributes.Count == 0)
                {
                    response = await SnsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest
                    {
                        Token = registrationId,
                        PlatformApplicationArn = arn,
                        CustomUserData         = userData
                    });

                    Settings.Arnsns = response.EndpointArn;
                }
                else if (results.Attributes["Token"] != registrationId)
                {
                    var updateToken = new SetEndpointAttributesRequest();
                    updateToken.Attributes.Add("Token", registrationId);
                    updateToken.Attributes.Add("Enabled", "true");
                    updateToken.EndpointArn = Settings.Arnsns;

                    var update = await SnsClient.SetEndpointAttributesAsync(updateToken);
                }
            }

            _endpointArn = response.EndpointArn;
        }