/// <summary>Pushes the message asynchronous.</summary> /// <param name="request">The request.</param> /// <returns></returns> /// <exception cref="NotSupportedException">Unknown PNS type</exception> public async Task <IActionResult> PushMessageAsync(PushNotification request) { NotificationOutcome outcome; // Send notifications by deviceId switch (request.Platform.ToLower()) { case "fcm": { outcome = await nhClient.SendDirectNotificationAsync(CreateFcmNotification(request.Message), request.Handle); break; } case "apns": { outcome = await nhClient.SendDirectNotificationAsync(CreateApnsNotification(request.Message), request.Handle); break; } default: { throw new NotSupportedException("Unknown PNS type"); } } logger.LogInformation($"Send notification result: {outcome.State}; Success: {outcome.Success}"); return(new OkResult()); }
private async Task SendDirectNotificationAsync_SendDirectFcmBatchNotification_GetSuccessfulResultBack() { LoadMockData(); var notification = new FcmNotification("{\"data\":{\"message\":\"Message\"}}"); var notificationResult = await _hubClient.SendDirectNotificationAsync(notification, new[] { _configuration["FcmDeviceToken"] }); Assert.Equal(NotificationOutcomeState.Enqueued, notificationResult.State); RecordTestResults(); }
private async Task SendDirectNotificationAsync_SendDirectAppleNotification_GetSuccessfulResultBack() { LoadMockData(); var notification = new AppleNotification("{\"aps\":{\"alert\":\"alert!\"}}"); var notificationResult = await _hubClient.SendDirectNotificationAsync(notification, _configuration["AppleDeviceToken"]); Assert.Equal(NotificationOutcomeState.Enqueued, notificationResult.State); RecordTestResults(); }
public async Task RetryPolicyRetriesOnTransientErrorInSend(HttpStatusCode errorCode) { _mockHttp.Expect("https://sample.servicebus.windows.net/hub-name/messages") .Respond(errorCode); _mockHttp.Expect("https://sample.servicebus.windows.net/hub-name/messages") .Respond(HttpStatusCode.OK); await _nhClient.SendDirectNotificationAsync(new FcmNotification("{}"), "123"); _mockHttp.VerifyNoOutstandingExpectation(); }
public static async Task <NotificationOutcome> SendDirectAsync(NotificationHubClient _hub, string notice, string token) { var notification = new FcmNotification(notice); var ret = await _hub.SendDirectNotificationAsync(notification, token); await LogFeedback(_hub, ret.NotificationId); return(ret); }
public async Task RetryPolicyGivesUpAfterTimeout() { _nhClient = new NotificationHubClient(_connectionString, _hubName, new NotificationHubSettings { MessageHandler = _mockHttp, RetryOptions = new NotificationHubRetryOptions { Delay = TimeSpan.FromMilliseconds(10), MaxRetries = 1 } }); _mockHttp.Expect("https://sample.servicebus.windows.net/hub-name/messages") .Throw(new TimeoutException()); _mockHttp.Expect("https://sample.servicebus.windows.net/hub-name/messages") .Throw(new TimeoutException()); _mockHttp.Expect("https://sample.servicebus.windows.net/hub-name/messages") .Respond(HttpStatusCode.OK); await Assert.ThrowsAsync <TimeoutException>(() => _nhClient.SendDirectNotificationAsync(new FcmNotification("{}"), "123")); }
/// <summary> /// Push送信 /// </summary> /// <param name="notification">GCM/WINDOWS用に設定済みの通知</param> /// <param name="deviceTokenList">デバイストークン</param> /// <returns></returns> private async Task SendNotificationAsync(Notification notification, List <string> deviceTokenList) { try { // TODO Define the notification hub. NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(MS_NotificationHubConnectionString, MS_NotificationHubName); //await hub.SendDirectNotificationAsync(notification, deviceTokenList); // リストで送信しようとするとtierで例外となるためループ処理する foreach (var item in deviceTokenList) { var notificationType = (notification is GcmNotification) ? "GCM" : "WNS"; System.Diagnostics.Debug.WriteLine($"-- SendNotificationAsync -- {notificationType}"); var notificationOutcome = await hub.SendDirectNotificationAsync(notification, item); } } catch (Exception) { throw; } }