public async Task <RegisterSuccessfulResult> RegisterSubscriberAsync(RegisterSubscriberModel model) { this.model = model; DynamoSubscriber subscriber = await subscribersTableOperator.GetSubscriberAsync(model.UserId, model.Token); // AWS SNS Mobile Push Algorithm // Check: http://docs.aws.amazon.com/sns/latest/dg/mobile-platform-endpoint.html bool updateNeeded = false; bool createNeeded = (subscriber == null); if (createNeeded) { subscriber = await CreateSubscriberAsync(null); createNeeded = false; } // Look up the endpoint and make sure the data in it is current, even if it was just created try { GetEndpointAttributesRequest geaRequest = new GetEndpointAttributesRequest(); geaRequest.EndpointArn = subscriber.EndpointARN; GetEndpointAttributesResponse geaResponse = await snsClient.GetEndpointAttributesAsync(geaRequest); updateNeeded = !geaResponse.Attributes["Token"].Equals(subscriber.NotificationToken) || !geaResponse.Attributes["Enabled"].Equals("true", StringComparison.OrdinalIgnoreCase); } catch (NotFoundException) { // We had a stored ARN, but the endpoint associated with it disappeared. Recreate it. createNeeded = true; } if (createNeeded) { subscriber = await CreateSubscriberAsync(subscriber); } if (updateNeeded) { // Endpoint is out of sync with the current data. Update the token and enable it. Dictionary <string, string> attrs = new Dictionary <string, string>(); attrs["Token"] = subscriber.NotificationToken; attrs["Enabled"] = "true"; SetEndpointAttributesRequest seaRequest = new SetEndpointAttributesRequest(); seaRequest.Attributes = attrs; seaRequest.EndpointArn = subscriber.EndpointARN; await snsClient.SetEndpointAttributesAsync(seaRequest); } if (tagOperator.IsTaggingAvailable()) { await tagOperator.SubscribeToTagsAsync(subscriber, model.Tags); } RegisterSuccessfulResult result = new RegisterSuccessfulResult(); result.EndpointArn = subscriber.EndpointARN; return(result); }
public DynamoSubscriber(RegisterSubscriberModel model, string endpointArn, TimeSpan?ttl) { this.UserId = model.UserId; this.NotificationToken = model.Token; this.DeviceId = model.DeviceId; this.Platform = model.Platform; this.EndpointARN = endpointArn; this.UpdatedDateTime = DateTime.UtcNow; if (ttl.HasValue) { this.ttl = DateTime.UtcNow.Add(ttl.Value); } }