private static async Task TrackAndSendNotificationsAsync(Sortation sortation, NotificationSubscription subscription) { // In a realistic case, some other backend process would track // sortation delivery progress and send us notifications when it // changes. Since we don't have any such process here, fake it. //await Task.Delay(PurchaseItem.PreparationDuration); //await SendNotificationAsync(sortation, subscription, "Your sortation has been dispatched!"); //await Task.Delay(PurchaseItem.DeliveryDuration); //await SendNotificationAsync(sortation, subscription, "Your sortation is now delivered. Enjoy!"); }
public async Task <Sortation> SaveAsync(Sortation sortation) { if (sortation.Id == 0) { var sortations = await ListAsync(sortation.CategoryId); sortation.Order = sortations.Count; _sortationService.Create(sortation); } else { _sortationService.Update(sortation); } await _sortationService.SaveChangesAsync(); return(sortation); }
public async Task <ActionResult <int> > SaveSortation(Sortation sortation) { // Enforce existence of Pizza.SpecialId and Topping.ToppingId // in the database - prevent the submitter from making up // new specials and toppings _db.Sortations.Attach(sortation); await _db.SaveChangesAsync(); // In the background, send push notifications if possible var subscription = await _db.NotificationSubscriptions.Where(e => e.UserId == GetUserId()).SingleOrDefaultAsync(); if (subscription != null) { _ = TrackAndSendNotificationsAsync(sortation, subscription); } return(sortation.Id); }
private static async Task SendNotificationAsync(Sortation sortation, NotificationSubscription subscription, string message) { // For a real application, generate your own var publicKey = "BLC8GOevpcpjQiLkO7JmVClQjycvTCYWm6Cq_a7wJZlstGTVZvwGFFHMYfXt6Njyvgx_GlXJeo5cSiZ1y4JOx1o"; var privateKey = "OrubzSz3yWACscZXjFQrrtDwCKg-TGFuWhluQ2wLXDo"; var pushSubscription = new PushSubscription(subscription.Url, subscription.P256dh, subscription.Auth); var vapidDetails = new VapidDetails("mailto:<*****@*****.**>", publicKey, privateKey); var webPushClient = new WebPushClient(); try { var payload = JsonSerializer.Serialize(new { message, url = $"mysortations/{sortation.Id}", }); await webPushClient.SendNotificationAsync(pushSubscription, payload, vapidDetails); } catch (Exception ex) { Console.Error.WriteLine("Error sending push notification: " + ex.Message); } }
public void Update(Sortation sortation) { }
public void Create(Sortation sortation) { }