private async void registrarPush(NSData token) { var access_key = "AKIAJLTJVDVD3ECKFSBQ"; var secret_key = "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"; var snsClient = new AmazonSimpleNotificationServiceClient(access_key, secret_key, RegionEndpoint.USWest2); var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", ""); Console.WriteLine("token=" + deviceToken + "__"); if (!string.IsNullOrEmpty(deviceToken)) { //register with SNS to create an endpoint ARN Console.WriteLine("token=" + deviceToken); var response = await snsClient.CreatePlatformEndpointAsync( new CreatePlatformEndpointRequest { CustomUserData = "*****@*****.**", Token = deviceToken, PlatformApplicationArn = "arn:aws:sns:us-west-2:490523474570:app/APNS_SANDBOX/idd_ios_dev" /* insert your platform application ARN here */ }); Console.WriteLine("arn_endoint ios" + response.EndpointArn + "__"); } }
private async void HandleRegistration(Intent intent) { string registrationId = intent.GetStringExtra("registration_id"); string error = intent.GetStringExtra("error"); string unregistration = intent.GetStringExtra("unregistered"); CognitoAWSCredentials credentials = new Authenticator().getCredentials(); snsClient = new AmazonSimpleNotificationServiceClient(credentials, RegionEndpoint.USEast1); if (string.IsNullOrEmpty(error)) { var response = await snsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest { Token = registrationId, PlatformApplicationArn = Keys.GCMEndpointARN /* insert your platform application ARN here */ }); var endpoint = response.EndpointArn; var subscribeResponse = await snsClient.SubscribeAsync(new SubscribeRequest { TopicArn = Keys.AppTopicARN, Endpoint = endpoint, Protocol = "application" }); } }
public override async void RegisteredForRemoteNotifications(UIApplication application, NSData token) { var deviceToken = token.Description.Replace("<", "").Replace(">", "").Replace(" ", ""); if (!string.IsNullOrEmpty(deviceToken)) { //register with SNS to create an endpoint ARN var response = await snsClient.CreatePlatformEndpointAsync( new CreatePlatformEndpointRequest { Token = deviceToken, PlatformApplicationArn = Keys.APNSEndpointARN /* insert your platform application ARN here */ }); var endpoint = response.EndpointArn; var subscribeResponse = await snsClient.SubscribeAsync(new SubscribeRequest { TopicArn = Keys.AppTopicARN, Endpoint = endpoint, Protocol = "application" }); } }
async void SendRegistrationToAppServer(int userId, string email, string deviceToken) { var access_key = "AKIAJLTJVDVD3ECKFSBQ"; var secret_key = "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"; var snsClient = new AmazonSimpleNotificationServiceClient(access_key, secret_key, RegionEndpoint.USWest2); //TODO cambiar en paso a produccion var applicationArn = "arn:aws:sns:us-west-2:490523474570:app/APNS_SANDBOX/idd_ios_dev"; Console.WriteLine("token=" + deviceToken + "__"); if (!string.IsNullOrEmpty(deviceToken)) { //register with SNS to create an endpoint ARN Console.WriteLine("token=" + deviceToken); var response = await snsClient.CreatePlatformEndpointAsync( new CreatePlatformEndpointRequest { CustomUserData = email, Token = deviceToken, PlatformApplicationArn = applicationArn /* insert your platform application ARN here */ }); Console.WriteLine("arn_endoint ios" + response.EndpointArn + "__"); var responseApi = await IDD.UserService.PostRegisterSNSDevice(userId, response.EndpointArn, applicationArn); if (responseApi != null) { if (responseApi["codigo"].ToString() == "1") { var s = responseApi["data"].ToString(); Console.WriteLine("se guardo correctmente el token" + userId.ToString() + "_" + response.EndpointArn); //ISharedPreferences prefs = Application.Context.GetSharedPreferences("PREF_NAME", FileCreationMode.Private); //ISharedPreferencesEditor editor = prefs.Edit(); //editor.PutString("endpoint_arn", response.EndpointArn); //editor.Apply(); } } } }
async void SendRegistrationToAppServer(int userId, string email, string token) { var access_key = "AKIAJLTJVDVD3ECKFSBQ"; var secret_key = "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"; var applicationArn = "arn:aws:sns:us-west-2:490523474570:app/GCM/idd_android"; var snsClient = new AmazonSimpleNotificationServiceClient(access_key, secret_key, RegionEndpoint.USWest2); Console.WriteLine(snsClient.ToString()); var response = await snsClient.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest { CustomUserData = email, Token = token, PlatformApplicationArn = applicationArn }); var responseApi = await IDD.UserService.PostRegisterSNSDevice(userId, response.EndpointArn, applicationArn); if (responseApi != null) { if (responseApi["codigo"].ToString() == "1") { var s = responseApi["data"].ToString(); Console.WriteLine("se guardo correctmente el token" + userId.ToString() + "_" + response.EndpointArn); ISharedPreferences prefs = Application.Context.GetSharedPreferences("PREF_NAME", FileCreationMode.Private); ISharedPreferencesEditor editor = prefs.Edit(); editor.PutString("endpoint_arn", response.EndpointArn); editor.Apply(); } } Console.WriteLine("endpoint arn___" + response.EndpointArn + "___"); }
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); }
/** * 1. register endpoint */ public async Task <string> CreateEndpoint(String deviceToken, Platform platformAppArn, MobileAppForSports mobApp) { String platformApplicationArn = ""; switch (platformAppArn) { case Platform.APNS: if (IsSandboxMode) { switch (mobApp) { case MobileAppForSports.Basketball: platformApplicationArn = platformArnAPNS_SANDBOX_Basketball; break; case MobileAppForSports.Netball: platformApplicationArn = platformArnAPNS_SANDBOX_Netball; break; case MobileAppForSports.Volleyball: platformApplicationArn = platformArnAPNS_SANDBOX_Volleyball; break; case MobileAppForSports.Waterpolo: platformApplicationArn = platformArnAPNS_SANDBOX_Waterpolo; break; } } else { switch (mobApp) { case MobileAppForSports.Basketball: platformApplicationArn = platformArnAPNS_Basketball; break; case MobileAppForSports.Netball: platformApplicationArn = platformArnAPNS_Netball; break; case MobileAppForSports.Volleyball: platformApplicationArn = platformArnAPNS_Volleyball; break; case MobileAppForSports.Waterpolo: platformApplicationArn = platformArnAPNS_Waterpolo; break; } } break; case Platform.GCM: if (IsSandboxMode) { platformApplicationArn = platformArnGCM_TEST; } else { platformApplicationArn = platformArnGCM; } break; } try { var resp = await client.CreatePlatformEndpointAsync(new CreatePlatformEndpointRequest { Token = deviceToken, PlatformApplicationArn = platformApplicationArn }); return(resp.EndpointArn); } catch (Exception e) { log.ErrorFormat("Error in function:CreateEndpoint. Error was: {0}", e.ToString()); } return(null); }