public string createPlatformApplicationAndAttachToTopic(string deviceToken, string username)
        {
            if (deviceToken == null || username == null)
            {
                throw new ArgumentException("Passing null device token or username");
            }

            var topicRequest = new CreateTopicRequest
            {
                Name = username + "_myTopic"
            };

            var topicResponse = snsClient.CreateTopic(topicRequest);


            var createPlatformApplicationRequest = new CreatePlatformApplicationRequest
            {
                // Platform Credential is the SERVER API KEY FOR GCM
                Attributes = new Dictionary <string, string>()
                {
                    { "PlatformCredential", API_KEY }, { "EventEndpointCreated", topicResponse.TopicArn }
                },
                Name     = username + "_platform",
                Platform = "GCM"
            };

            var createPlatformResponse = snsClient.CreatePlatformApplication(createPlatformApplicationRequest);

            string platformApplicationArn = createPlatformResponse.PlatformApplicationArn;

            var request1 = new CreatePlatformEndpointRequest
            {
                CustomUserData         = "",
                PlatformApplicationArn = platformApplicationArn,
                Token = deviceToken
            };

            // Getting the endpoint result
            // It contains Endpoint ARN that needs to be subscripted to a topic
            var createPlatformEndpointResult = snsClient.CreatePlatformEndpoint(request1);


            try
            {
                snsClient.Subscribe(new SubscribeRequest
                {
                    Protocol = "application",
                    TopicArn = topicResponse.TopicArn,
                    Endpoint = createPlatformEndpointResult.EndpointArn
                });
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                return(null);
            }

            return(topicResponse.TopicArn);
        }
Example #2
0
        public string CreateEndpoint(string registrationid, string description, string aplicationarn)
        {
            try
            {
                var endpoint1 = GetEndpoint(registrationid, String.Empty);

                if (endpoint1 != null)
                {
                    throw new ArgumentException("registrationid");
                }

                var endpoint2 = GetEndpoints(description);

                if (endpoint2 != null)
                {
                    if (endpoint2.Count == 1)
                    {
                        return(endpoint2.FirstOrDefault());
                    }

                    if (endpoint2.Count > 1)
                    {
                        throw new ArgumentException("description");
                    }
                }

                var app = GetApplication(aplicationarn, string.Empty);
                if (string.IsNullOrEmpty(app))
                {
                    throw new ArgumentException("aplicationtoken");
                }

                using (var snsclient = new AmazonSimpleNotificationServiceClient(_accesskey, _secretkey))
                {
                    var request = new CreatePlatformEndpointRequest()
                    {
                        CustomUserData         = description,
                        Token                  = registrationid,
                        PlatformApplicationArn = aplicationarn
                    };

                    var result = snsclient.CreatePlatformEndpoint(request);

                    return(result?.EndpointArn);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("CreateEndpoint " + ex.Message);
            }
        }
Example #3
0
        public string CreatePushEndpoint(string registrationId)
        {
            if (string.IsNullOrEmpty(registrationId))
            {
                throw new Exception("Registration id was null");
            }

            var result = _client.CreatePlatformEndpoint(new CreatePlatformEndpointRequest()
            {
                PlatformApplicationArn = "arn:aws:sns:ap-southeast-2:659169760454:app/GCM/Valet", // TODO consider making this a constant
                Token = registrationId
            });

            return(result.EndpointArn);
        }
Example #4
0
        public static void SNSMobilePushAPIsCreatePlatformEndpoint()
        {
            #region SNSMobilePushAPIsCreatePlatformEndpoint
            var snsClient = new AmazonSimpleNotificationServiceClient();

            var request = new CreatePlatformEndpointRequest
            {
                CustomUserData         = "Any arbitrary data can go here",
                PlatformApplicationArn = "arn:aws:sns:us-east-1:80398EXAMPLE:" +
                                         "app/GCM/TimeCardProcessingApplication",
                Token = "APBTKzPGlCyT6E6oOfpdwLpcRNxQp5vCPFiF" +
                        "eru9oZylc22HvZSwQTDgmmw9WdNlXMerUPEXAMPLE"
            };

            snsClient.CreatePlatformEndpoint(request);
            #endregion
        }
        public string[] createPlatformApplication(string deviceToken, string username)
        {
            if (deviceToken == null || username == null)
            {
                throw new ArgumentException("Passing null device token or username");
            }

            var createPlatformApplicationRequest = new CreatePlatformApplicationRequest
            {
                // Platform Credential is the SERVER API KEY FOR GCM
                Attributes = new Dictionary <string, string>()
                {
                    { "PlatformCredential", API_KEY }
                },
                Name     = username + "_platform",
                Platform = "GCM"
            };

            var createPlatformResponse = snsClient.CreatePlatformApplication(createPlatformApplicationRequest);

            string platformApplicationArn = createPlatformResponse.PlatformApplicationArn;

            var request1 = new CreatePlatformEndpointRequest
            {
                CustomUserData         = "",
                PlatformApplicationArn = platformApplicationArn,
                Token = deviceToken
            };

            // Getting the endpoint result
            // It contains Endpoint ARN that needs to be subscripted to a topic
            var createPlatformEndpointResult = snsClient.CreatePlatformEndpoint(request1);


            string[] arr = new string[2] {
                "", ""
            };
            arr[1] = createPlatformEndpointResult.EndpointArn;
            arr[0] = platformApplicationArn;

            return(arr);
        }