/// <summary> /// Sends a PushSharp notification to the service end-point. /// </summary> /// <param name="notification">The notification to send.</param> /// <returns>The task and result of the operation.</returns> protected async Task <PushNotificationResult> SendPushSharpNotification(TPushSharpNotification notification) { if (this.serviceConnection == null) { throw new InvalidOperationException("The end-point has not been connected."); } try { // TODO: it appears we can optimize this by creating our own connection interface that does not throw the exception but instead returns it // the underlying APNS service connection uses an APNS connection object that returns an exception but the service connection object throws it await this.serviceConnection.Send(notification).ConfigureAwait(false); return(PushNotificationResult.Success(this.EndPointName)); } catch (Exception ex) { PushNotificationResult result = this.HandleFailedNotificationResult(notification, ex); if (result != null) { return(result); } throw; } }
private async Task SendAsync <TAccountKey>(NotificationAccount <TAccountKey> account, MobileDevice mobileDevice, PushNotification notification) { PushNotificationResult result = await this.mobileNotificationServiceClient.SendNotification(mobileDevice, notification).ConfigureAwait(false); if (result.WasSuccessful) { this.OnSuccess(notification, account.AccountId.ToString(), mobileDevice.DeviceId, result.EndpointName); } else { this.OnFailed( notification, NotificationErrorCode.RouteFailure, account.AccountId.ToString(), result.ErrorMessage, mobileDevice.DeviceId, result.EndpointName); } }
/// <summary> /// Handles a failed notification and returns the result for the failure or null if the exception should be rethrown. /// </summary> /// <param name="notification">The notification that failed to send.</param> /// <param name="ex">The exception that occurred.</param> /// <returns>A notification result or null.</returns> protected override PushNotificationResult HandleFailedNotificationResult(ApnsNotification notification, Exception ex) { ApnsNotificationException notificationException = ex as ApnsNotificationException; if (notificationException != null) { if (notificationException.ErrorStatusCode == ApnsNotificationErrorStatusCode.ConnectionError) { return (PushNotificationResult.Failed( this.EndPointName, "Connection error: " + ex.Message)); } return (PushNotificationResult.Failed( this.EndPointName, "Failed to send notification (" + notificationException.Notification.Identifier + ") with status (" + notificationException.ErrorStatusCode + ") and error message: " + ex.Message)); } return(null); }