public async Task RegisterUserNotifications() { string deviceToken = GetDeviceToken(); var notificationsSubscriptions = GetNotificationsSubscriptions(); if (string.IsNullOrEmpty(deviceToken)) { return; } var credentials = new BasicAWSCredentials( AppSettings.Instance.AwsKey, AppSettings.Instance.AwsSecret ); var client = new AmazonSimpleNotificationServiceClient( credentials, Amazon.RegionEndpoint.EUWest1 ); if (string.IsNullOrEmpty(notificationsSubscriptions.ApplicationEndPoint) || notificationsSubscriptions.DeviceToken != deviceToken) { // ********************************************** // de-register old endpoint and all subscriptions if (!string.IsNullOrEmpty(notificationsSubscriptions.ApplicationEndPoint)) { try { var response = await client.DeleteEndpointAsync(new DeleteEndpointRequest { EndpointArn = notificationsSubscriptions.ApplicationEndPoint }); if (response.HttpStatusCode != System.Net.HttpStatusCode.OK) { ShowAlert("Debug", $"Error eliminando endpoint: {response.HttpStatusCode}"); } } catch { /*Silent error in case endpoint doesn´t exist */ } notificationsSubscriptions.ApplicationEndPoint = null; foreach (var sub in notificationsSubscriptions.Subscriptions) { try { await client.UnsubscribeAsync(sub.Value); } catch { /*Silent error in case endpoint doesn´t exist */ } } notificationsSubscriptions.Subscriptions.Clear(); } // register with SNS to create a new endpoint var endPointResponse = await client.CreatePlatformEndpointAsync( new CreatePlatformEndpointRequest { Token = deviceToken, PlatformApplicationArn = Device.RuntimePlatform == Device.iOS ? AppSettings.Instance.AwsPlatformApplicationArnIOS : AppSettings.Instance.AwsPlatformApplicationArnAndroid } ); if (endPointResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { ShowAlert("Debug", $"Error registrando endpoint: {endPointResponse.HttpStatusCode}, {endPointResponse.ResponseMetadata}"); } // Save device token and application endpoint created notificationsSubscriptions.DeviceToken = deviceToken; notificationsSubscriptions.ApplicationEndPoint = endPointResponse.EndpointArn; } // Retrieve subscriptions var subscriptions = await AudioLibrary.Instance.GetUserSubscriptions(false); if (subscriptions == null) { subscriptions = new UserSubscriptions { Subscriptions = new List <Models.Api.Subscription>() } } ; // Register non existings subscriptions var subscriptionsCodes = subscriptions.Subscriptions.Select(s => s.Code).ToList(); foreach (var code in subscriptionsCodes) { if (!notificationsSubscriptions.Subscriptions.ContainsKey(code)) { var topicArn = AppSettings.Instance.AwsTopicArn; topicArn += string.IsNullOrEmpty(code) ? "" : $"-{code}"; if (!await TopicExists(topicArn, client)) { var topicResponse = await client.CreateTopicAsync(new CreateTopicRequest { Name = $"{AppSettings.Instance.AwsTopicName}-{code}" }); if (topicResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { ShowAlert("Debug", $"Error creando topic: {topicResponse.HttpStatusCode}, {topicResponse.ResponseMetadata}"); } topicArn = topicResponse.TopicArn; } // Subscribe var subscribeResponse = await client.SubscribeAsync(new SubscribeRequest { Protocol = "application", Endpoint = notificationsSubscriptions.ApplicationEndPoint, TopicArn = topicArn }); if (subscribeResponse.HttpStatusCode != System.Net.HttpStatusCode.OK) { ShowAlert("Debug", $"Error creando suscripción: {subscribeResponse.HttpStatusCode}, {subscribeResponse.ResponseMetadata}"); } // Add to the list notificationsSubscriptions.Subscriptions.Add(code, subscribeResponse.SubscriptionArn); } } // Remove subscriptions not in user list var currentSubscriptions = notificationsSubscriptions.Subscriptions.ToList(); foreach (var subs in currentSubscriptions) { if (!subscriptionsCodes.Contains(subs.Key)) { try { await client.UnsubscribeAsync(subs.Value); } catch { /*Silent error in case endpoint doesn´t exist */ } notificationsSubscriptions.Subscriptions.Remove(subs.Key); } } // Save notifications subscriptions await SaveNotificationsSubscriptions(notificationsSubscriptions); }