private async Task ScheduleNotificationAsync_SendAppleNativeNotification_GetSuccessfulResultBack() { LoadMockData(); var notification = new AppleNotification("{\"aps\":{\"alert\":\"alert!\"}}"); notification.Expiry = DateTime.Now.AddDays(1); notification.Priority = 5; var notificationResult = await _hubClient.ScheduleNotificationAsync(notification, DateTimeOffset.Now.AddDays(1)); Assert.NotNull(notificationResult.ScheduledNotificationId); RecordTestResults(); }
public async Task <HttpResponseMessage> SchedulePushNotificationAlert(int userId) { var schedulerepo = new UserTrackingScheduleRepository(); var userrepo = new UserRepository(); var user = userrepo.Get(userId); NotificationHubClient hub = NotificationHubClient .CreateClientFromConnectionString("<connection string with full access>", "<hub name>"); var schedule = user.UserTrackingSchedules.Where(_ => _.Enabled).FirstOrDefault(); if (schedule != null && schedule.Enabled) { Notification notification = new AppleNotification("{\"aps\":{\"alert\":\"Happy birthday!\"}}"); var scheduledTime = new DateTimeOffset(DateTime.Today.AddDays(schedule.ScheduledDays - 1), new TimeSpan(schedule.ScheduledHours, 0, 0)); var scheduled = await hub.ScheduleNotificationAsync(notification, scheduledTime); schedule.ScheduledNotificationId = scheduled.ScheduledNotificationId; schedulerepo.AddEdit(schedule); } return(Request.CreateResponse(HttpStatusCode.OK)); }
/// <summary> /// 新しい通知スケジュールを作成します /// </summary> public async Task <IHttpActionResult> Post(ScheduleFormModel data) { // 送信されたデータが正しいか検証する if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var notifications = new List <Notification>(); // 送信するペイロードを作成 if (data.Windows > 0) { notifications.Add(Payloads.Windows.Create(data.Windows, data.Message)); } if (data.WindowsPhone > 0) { notifications.Add(Payloads.WindowsPhone.Create(data.WindowsPhone, data.Message)); } if (data.Apple > 0) { notifications.Add(Payloads.Apple.Create(data.Apple, data.Message)); } if (data.Android > 0) { notifications.Add(Payloads.Android.Create(data.Android, data.Message)); } // 送信先が 1 つもない場合にはエラー if (notifications.Count == 0) { return(BadRequest("Required Platform select.")); } // 通知ハブへ通知リクエストを非同期で送信 var tasks = notifications.Select(p => _client.ScheduleNotificationAsync(p, data.ScheduledOn.Value, data.TagExpression)); // 全ての通知リクエストの完了を待機する var results = await Task.WhenAll(tasks); foreach (var result in results) { // スケジュールログを DB に保存 var scheduleLog = new ScheduleLog { NotificationId = result.ScheduledNotificationId, Platform = result.Payload.GetPlatform(), Message = data.Message, TagExpression = data.TagExpression, ScheduledOn = data.ScheduledOn.Value, CreatedOn = DateTimeOffset.UtcNow }; _context.ScheduleLogs.Add(scheduleLog); } await _context.SaveChangesAsync(); return(Ok()); }