public async Task <AzureNotificationHub> GetAzureNotificationHubEndpoint(string appId) { AzureNotificationHub hub = new AzureNotificationHub(); using (var conn = new SqlConnection(_connectionString)) { var sql = $"SELECT AppId, Endpoint, HubName FROM AzureNotificationHubs WHERE AppId = '{appId}'"; using (var cmd = new SqlCommand(sql, conn)) { await conn.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(); if (reader.HasRows) { while (await reader.ReadAsync()) { hub.AppId = appId; hub.Endpoint = reader[1].ToString(); hub.HubName = reader[2].ToString(); } } } } return(hub); }
public async Task <string> PostAsync([FromBody] PushRegistration registration) { #region input validation if (registration == null) { throw new ArgumentException("Check one or more of your arguments"); } if (string.IsNullOrEmpty(registration.AppId)) { throw new ArgumentException("Please provide a valid AppId"); } if (string.IsNullOrEmpty(registration.DeviceToken)) { throw new ArgumentException("Please provide a valid DeviceToken"); } if (registration.Platform == Platform.none) { throw new ArgumentException("Platform can only be iOS, Android or Windows"); } #endregion string registrationId = null; //registration.UserId = "*****@*****.**"; //registration.UserId = RequestContext.Principal.Identity.Name.ToLower(); if (registration.Tags == null) { registration.Tags = new List <string>(); } registration.Tags.Add($"UserId:{registration.UserId}"); AzureNotificationHub hub = await _dbRepo.GetAzureNotificationHubEndpoint(registration.AppId); if (string.IsNullOrEmpty(hub.Endpoint) && string.IsNullOrEmpty(hub.HubName)) { throw new Exception($"Unable to find an enpoint for appId = '{registration.AppId}'"); } _nhRepo = new NotificationHubRepository(hub.Endpoint, hub.HubName); registrationId = await _nhRepo.Upsert(registration); registration.RegistrationId = registrationId; var success = await _dbRepo.Upsert(registration); //await Clean(registration); return(registrationId); }
public async Task <IEnumerable <RegistrationDescription> > GetAsync(string appId) { AzureNotificationHub hub = await _dbRepo.GetAzureNotificationHubEndpoint(appId); if (string.IsNullOrEmpty(hub.Endpoint) && string.IsNullOrEmpty(hub.HubName)) { throw new Exception($"Unable to find an enpoint for appId = '{appId}'"); } _nhRepo = new NotificationHubRepository(hub.Endpoint, hub.HubName); var retval = await _nhRepo.Get(); return(retval); }
public async Task <HttpResponseMessage> Post([FromBody] SendPayload sendPayload) { HttpStatusCode ret = HttpStatusCode.InternalServerError; List <NotificationOutcome> outcomes = null; AzureNotificationHub hub = await _dbRepo.GetAzureNotificationHubEndpoint(sendPayload.AppId); if (string.IsNullOrEmpty(hub.Endpoint) && string.IsNullOrEmpty(hub.HubName)) { throw new Exception($"Unable to find an enpoint for appId = '{sendPayload.AppId}'"); } var _nhRepo = new NotificationHubRepository(hub.Endpoint, hub.HubName); if (!string.IsNullOrEmpty(sendPayload.UserId)) { var pns = await _dbRepo.GetPns(sendPayload.AppId, sendPayload.UserId); outcomes = await _nhRepo.Send(sendPayload, pns); } else { outcomes = await _nhRepo.Send(sendPayload); } if (outcomes != null) { ret = HttpStatusCode.OK; foreach (var outcome in outcomes) { if ((outcome.State == NotificationOutcomeState.Abandoned) || (outcome.State == NotificationOutcomeState.Unknown)) { ret = HttpStatusCode.InternalServerError; break; } } } return(Request.CreateResponse(ret, "{\"result\":\"OK\"}")); }
private void PushRegisterMethod(object obj) { AzureNotificationHub.RegisterNotificationHub(); PushButtonLabel = "** Notifications Registered **"; }