private async Task <DynamoSubscriber> CreateDynamoSubscriber(string endpointArn) { var subscriber = new DynamoSubscriber(model, endpointArn, Provider.SubscriberTimeToLive); await subscribersTableOperator.AddSubscriberAsync(subscriber); return(subscriber); }
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 async Task SubscribeToTagsAsync(DynamoSubscriber subscriber, List <PNAttributedTag> tags) { if (tags == null) { return; } await Task.WhenAll(tags.Select(tag => SubscribeToTagAsync(subscriber, tag))); // foreach (var tag in tags) // { // await SubscribeToTagAsync(subscriber, tag); // } }
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); }
public async Task SubscribeToTagAsync(DynamoSubscriber subscriber, PNAttributedTag tag) { switch (tag.TagMethod) { case PNTagType.Iterative: await SubscribeToIterativeTagAsync(tag.Tag, subscriber.UserId, subscriber.NotificationToken, subscriber.EndpointARN, subscriber.Platform); break; case PNTagType.SNSTopic: await SubscribeToSNSTopicTagAsync(tag.Tag, subscriber.UserId, subscriber.NotificationToken, base.Provider.AppIdentifier, subscriber.EndpointARN); break; default: return; } }
public async Task <SwitchSubscriberResult> SwitchSubscriberAsync(SwitchSubscriberModel model) { DynamoSubscriber subscriber = await subscribersTableOperator.GetSubscriberAsync(model.PrevUserId, model.PrevToken); List <IDynamoTagEntry> tags = null; if (subscriber != null) { tags = await tagOperator.GetAllTagsForSubscribers(new List <Subscriber> { subscriber.Subscriber }); var unregisterResult = await unRegisterOperator.UnRegisterSubscriberAsync(subscriber, tags); } var atributedTags = tags?.Where(t => { if (model.TagsToIgnore?.Any(tIgnore => tIgnore.Tag.Equals(t.Tag)) ?? false) { return(false); } else { return(true); } }).Select(t => new PNAttributedTag { Tag = t.Tag, TagMethod = t.TagType }).ToList(); if (subscriber == null) { subscriber = new DynamoSubscriber(); } subscriber.UserId = model.NewUserId; subscriber.NotificationToken = model.NewToken; subscriber.Platform = model.Platform; await registerOperator.RegisterSubscriberAsync(new RegisterSubscriberModel(subscriber, atributedTags, model.ApplicationPlatformArn)); return(new SwitchSubscriberSuccessfulResult()); }
private async Task <PublishToSNSResult> PublishToSubscriberAsync(DynamoSubscriber subscriber) { PublishToSNSResult result; if (subscriber == null) { result = new PublishToSNSFailedResult(new SubscriberNotFoundException(subscriber.Subscriber.UserId, subscriber.Subscriber.Token)); return(result); // throw new SubscriberNotFoundException(subscriber.Subscriber.UserId, subscriber.Subscriber.Token); } if (IsTargetPlatformRestrictsSubscriber(model.TargetPlatform, subscriber.Platform)) { result = new PublishToSNSFailedResult(new PlatformUnmatchedException(model.TargetPlatform, subscriber.Platform)); return(result); // return new PublishToSNSFailedResult("TargetPlatform doesn't match with the subscribers platform"); } string message = base.SerializeMessageFromProperties(subscriber.Platform); if (string.IsNullOrEmpty(message)) { result = new PublishToSNSFailedResult(new SNSNotificationMessageNullException()); return(result); } // Console.WriteLine("SerializedMessage: " + message); result = await PublishToEndpointAsync(subscriber.EndpointARN, message); result.UserId = subscriber.UserId; if (result.ErrorAlias == ErrorReason.SNSEndpointDisabled || result.ErrorAlias == ErrorReason.SNSEndpointNotFound) { await unRegisterOperator.UnRegisterSubscriberAsync(subscriber.UserId, subscriber.NotificationToken); } return(result); }
public async Task <UnRegisterSubscriberSuccessfulResult> UnRegisterSubscriberAsync(DynamoSubscriber subscriber, List <IDynamoTagEntry> tags) { UnRegisterSubscriberSuccessfulResult result = new UnRegisterSubscriberSuccessfulResult(); if (subscriber != null) { if (tagOperator.IsTaggingAvailable()) { await tagOperator.RemoveTagSubscribers(tags); } DeleteEndpointRequest deReq = new DeleteEndpointRequest(); deReq.EndpointArn = subscriber.EndpointARN; await snsClient.DeleteEndpointAsync(deReq); await subscribersTableOperator.RemoveSubscriberAsync(subscriber); } else { result.NotRegistered = true; return(result); } return(result); }
public async Task <UnRegisterSubscriberSuccessfulResult> UnRegisterSubscriberAsync(DynamoSubscriber subscriber) { UnRegisterSubscriberSuccessfulResult result = new UnRegisterSubscriberSuccessfulResult(); List <IDynamoTagEntry> tags = new List <IDynamoTagEntry>(); if (tagOperator.IsTaggingAvailable()) { var subscribers = new List <Subscriber> { new Subscriber { UserId = subscriber.UserId, Token = subscriber.NotificationToken } }; tags = await tagOperator.GetAllTagsForSubscribers(subscribers); } return(await UnRegisterSubscriberAsync(subscriber, tags)); }
public async Task <UnRegisterSubscriberSuccessfulResult> UnRegisterSubscriberAsync(string userId, string token) { DynamoSubscriber entry = await subscribersTableOperator.GetSubscriberAsync(userId, token); return(await UnRegisterSubscriberAsync(entry)); }
public RegisterSubscriberModel(DynamoSubscriber model, List <PNAttributedTag> tags, string applicationPlatformArn) : base(model) { this.Platform = model.Platform; this.Tags = tags; this.ApplicationPlatformArn = applicationPlatformArn; }
public RegistrationModel(DynamoSubscriber model, bool consistentRead = false) { this.UserId = model.UserId; this.Token = model.NotificationToken; this.DeviceId = model.DeviceId; }
public async Task <PublishToSNSResult> PublishToSubscriberAsync(string token) { DynamoSubscriber subscriber = await subscriberTableOperator.GetSubscriberAsync(model.UserId, token); return(await PublishToSubscriberAsync(subscriber)); }
private async Task ReAssignTagsForUserWithNewSubscriber(DynamoSubscriber subscriber) { var currentTags = await tagOperator.GetAllTagsOfUserAsAtrributedTags(model.UserId); await tagOperator.SubscribeToTagsAsync(subscriber, currentTags); }