private async Task RegisterForPushNotification(string deviceToken) { try { var registration = new PushRegistration() { Handle = deviceToken, RegistrationId = RetrieveCurrentRegistrationId?.Invoke(), Platform = PushPlatform.WNS }; var botClientMobileAppClient = new BotClientMobileAppClient(endpointRouteDetails); var hubRegistrationResult = await botClientMobileAppClient.RegisterPushAsync(registration); if (hubRegistrationResult != null) { RegistrationSuccessful?.Invoke(hubRegistrationResult.RegistrationId); } else { RegistrationFailure?.Invoke(new Exception("Registration Failure")); } } catch (Exception ex) { RegistrationFailure?.Invoke(ex); Debug.WriteLine(ex.Message); } }
/// <summary> /// Method which, by making a call to the service, registers your device to push notifications hub. /// </summary> /// <param name="pushRegistration"></param> /// <returns></returns> public async Task<BuildIt.Web.Models.Results.HubRegistrationResult> RegisterPushAsync(PushRegistration pushRegistration) { if (endpointRouteDetails?.BaseServiceUrl == null) return null; BuildIt.Web.Models.Results.HubRegistrationResult hubRegistrationResult = null; try { var json = JsonConvert.SerializeObject(pushRegistration); using (var client = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Post, $"{endpointRouteDetails.BaseServiceUrl}/{endpointRouteDetails.ServiceAffix}/{endpointRouteDetails.RegisterPushRoute}"); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.JsonMimeType); if (IsMobileService) request.Content.Headers.Add("ZUMO-API-VERSION", "2.0.0"); var response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { var contentJson = await response.Content.ReadAsStringAsync(); hubRegistrationResult = JsonConvert.DeserializeObject<BuildIt.Web.Models.Results.HubRegistrationResult>(contentJson); } } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return hubRegistrationResult; }
/// <summary> /// /// </summary> /// <param name="pushRegistration"></param> /// <returns></returns> public async Task<string> CreateOrUpdateRegistrationAsync(PushRegistration pushRegistration) { UserRegistrationResult userRegistrationResult = await RegisterUserAsync(pushRegistration); if (userRegistrationResult == null) { return pushRegistration.RegistrationId; } RegistrationDescription registration = null; switch (pushRegistration.Platform) { case PushPlatform.APNS: registration = new AppleRegistrationDescription(pushRegistration.Handle); break; case PushPlatform.GCM: registration = new GcmRegistrationDescription(pushRegistration.Handle); break; case PushPlatform.WNS: registration = new WindowsRegistrationDescription(pushRegistration.Handle); break; default: //throw new HttpResponseException(HttpStatusCode.BadRequest); throw new Exception(); } registration.RegistrationId = userRegistrationResult.RegistrationId; if (pushRegistration.Tags?.Any() ?? false) { registration.Tags = new HashSet<string>(pushRegistration.Tags); } await notificationHub.CreateOrUpdateRegistrationAsync(registration); return registration.RegistrationId; }
/// <summary> /// /// </summary> /// <param name="pushRegistration"></param> /// <returns></returns> public async Task<JsonResult> Post(PushRegistration pushRegistration) { var res = new HubRegistrationResult(); try { res.RegistrationId = await notificationService.CreateOrUpdateRegistrationAsync(pushRegistration); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); res.ErrorMessage = ex.Message; } return Json(res); }
/// <summary> /// /// </summary> /// <param name="pushRegistration"></param> /// <returns></returns> public async Task DeregisterPushAsync(PushRegistration pushRegistration) { try { using (var client = new HttpClient()) { var json = JsonConvert.SerializeObject(pushRegistration); var request = new HttpRequestMessage(HttpMethod.Post, $"{endpointRouteDetails.BaseServiceUrl}/{endpointRouteDetails.ServiceAffix}/{endpointRouteDetails.DeregisterPushRoute}"); request.Content = new StringContent(json); request.Content.Headers.ContentType = new MediaTypeHeaderValue(Constants.JsonMimeType); if (IsMobileService) request.Content.Headers.Add("ZUMO-API-VERSION", "2.0.0"); var response = await client.SendAsync(request); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private async Task<UserRegistrationResult> RegisterUserAsync(PushRegistration pushRegistration) { UserRegistrationResult userRegistrationResult = null; if (notificationHub == null) return null; string newRegistrationId = null; if (pushRegistration.Handle != null) { var registrations = await notificationHub.GetRegistrationsByChannelAsync(pushRegistration.Handle, 100); foreach (RegistrationDescription registration in registrations) { if (newRegistrationId == null) { newRegistrationId = registration.RegistrationId; } else { await notificationHub.DeleteRegistrationAsync(registration); } } } if (newRegistrationId == null) { var registrationId = await notificationHub.CreateRegistrationIdAsync(); userRegistrationResult = new UserRegistrationResult() { RegistrationId = registrationId }; } else { userRegistrationResult = new UserRegistrationResult() { RegistrationId = newRegistrationId }; } return userRegistrationResult; }
/// <summary> /// /// </summary> /// <param name="pushRegistration"></param> /// <returns></returns> public async Task DeleteRegistrationAsync(PushRegistration pushRegistration) { try { await notificationHub.DeleteRegistrationAsync(pushRegistration.RegistrationId); } catch (Exception ex) { Debug.WriteLine(ex.Message); throw; } }
private async Task RegisterPushNotifications(string deviceToken) { try { var registration = new PushRegistration() { Handle = deviceToken, RegistrationId = Settings.Instance.RegistrationId, Platform = PushPlatform.GCM }; var hubRegistrationResult = await botClientMobileApp.RegisterPushAsync(registration); if (hubRegistrationResult != null) { MessagingCenter.Send(this, Constants.SuccessSubscriptionMsg, hubRegistrationResult.RegistrationId); } else { MessagingCenter.Send(this, Constants.FailureSubscriptionMsg, new Exception("Registration Failure")); } } catch (Exception ex) { Debug.WriteLine(ex.Message); MessagingCenter.Send(this, Constants.FailureSubscriptionMsg, ex); } }