public void TestSetVapidDetails() { client.SetVapidDetails(TestSubject, TestPublicKey, TestPrivateKey); var subscription = new PushSubscription(TestFirefoxEndpoint, TestPublicKey, TestPrivateKey); var message = client.GenerateRequestDetails(subscription, @"test payload"); var authorizationHeader = message.Headers.GetValues(@"Authorization").First(); var cryptoHeader = message.Headers.GetValues(@"Crypto-Key").First(); Assert.IsTrue(authorizationHeader.StartsWith(@"WebPush ")); Assert.IsTrue(cryptoHeader.Contains(@"p256ecdsa")); }
public WebPushPushNotificationService(IOptions <VapidSettings> optionsAccessor) { _options = optionsAccessor.Value; _pushClient = new WebPushClient(); _pushClient.SetVapidDetails(_options.Subject, _options.PublicKey, _options.PrivateKey); }
/// <summary> /// Sends a message to multiple devices. /// </summary> /// <param name="p_message">Message to send.</param> /// <param name="p_targetDevices">Target devices.</param> public Task SendMessage(MessageViewModel p_message, IEnumerable <Device> p_targetDevices) { WebPushClient v_webPushClient = new WebPushClient(); if (this.HasVapidPublicKey()) { v_webPushClient.SetVapidDetails(VapidDetails); } if (!String.IsNullOrWhiteSpace(GcmAPIKey)) { v_webPushClient.SetGcmApiKey(GcmAPIKey); } List <Task> v_pending = new List <Task>(); foreach (Device v_targetDevice in p_targetDevices) { string v_message = JsonConvert.SerializeObject(p_message); v_pending.Add(v_webPushClient.SendNotificationAsync(v_targetDevice, v_message)); } return(Task.WhenAll(v_pending)); }
public async Task <IActionResult> PushTest([FromBody] string testMessage) { var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(_vapidDetails); // send test notification var payload = new PushNotificationPayload { Msg = "Aurelia har falt. Vaktsentralen, ta kontakt å sjekk ut situasjon og meld neste aksjon", Icon = "[URL to an image to display in the notification]" }; try { await webPushClient.SendNotificationAsync(_pushSubscription, JsonConvert.SerializeObject(payload), _vapidDetails); } catch (WebPushException exception) { var statusCode = exception.StatusCode; return(new StatusCodeResult((int)statusCode)); } return(new OkResult()); }
public async Task <IActionResult> Create([FromBody] PushNotificationSubscription subscription) { var pushSubscription = new PushSubscription(subscription.Endpoint, subscription.Key, subscription.AuthSecret); var vapidDetails = new VapidDetails(_vapidSettings.Subject, _vapidSettings.PublicKey, _vapidSettings.PrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); //TODO; store pushsubscription for later use _pushSubscription = pushSubscription; _vapidDetails = vapidDetails; // send notification var payload = new PushNotificationPayload { Msg = "Thank you for subscribing", Icon = "[URL to an image to display in the notification]" }; try { await webPushClient.SendNotificationAsync(pushSubscription, JsonConvert.SerializeObject(payload), vapidDetails); } catch (WebPushException exception) { var statusCode = exception.StatusCode; return(new StatusCodeResult((int)statusCode)); } return(new OkResult()); }
public async Task <bool> Post([FromBody] PushNotificationModel subscription) { try { var pushSubscription = new PushSubscription(subscription.Endpoint, subscription.Key, subscription.AuthSecret); var vapidDetails = new VapidDetails("http://localhost:50973/forum", "BAdnuHOxwOFm_GV_NYG1CZOjddlrVfDbKobDFTTxQvgcGBhPI47gkxfEUdtgX2iO_x4PwUkyj-xS7Uke_UmIaqQ", "vrRVfvxyx4kIEYSfansI_eOI4a-HdTCJpa0EVmjLYnE"); ForumPostSubscriptions.AddSubscription(pushSubscription); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); var payload = new PushNotificationPayload { Msg = "Thank you for subscribing", Icon = "C:/Temp/icon192x192.png", }; string temp = JsonConvert.SerializeObject(payload); //await webPushClient.SendNotificationAsync(pushSubscription, temp, ); await webPushClient.SendNotificationAsync(pushSubscription, temp, vapidDetails); return(true); } catch (Exception ex) { return(false); } }
public async Task <IActionResult> SendNotificationAsync([FromBody] SendNotification model) { var vapidDetails = new VapidDetails(_settings.Subject, _settings.PublicKey, _settings.PrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); // send notification var payload = new PushNotificationPayload { Title = "Push Demo Title", Body = "Put some message in the body of the notification.", Icon = "assets/cazton-c.png", Vibrate = new List <int> { 100, 50, 100 } }; try { var subscription = _pushSusbscriptions.GetAll().FirstOrDefault(x => x.Endpoint == model.Endpoint); if (subscription != null) { await webPushClient.SendNotificationAsync(subscription, JsonConvert.SerializeObject(new { notification = payload }), vapidDetails); } } catch (WebPushException exception) { var statusCode = exception.StatusCode; } return(Ok()); }
public PushNotifcationService(IOptions <VapidSettings> vapidSettings, IOptions <CosmosDbConfiguration> cosmosDbOptions, DocumentClient client) { _client = client; _vapidSettings = vapidSettings.Value; _pushClient = new WebPushClient(); _cosmosConfig = cosmosDbOptions.Value; _pushClient.SetVapidDetails(_vapidSettings.Subject, _vapidSettings.PublicKey, _vapidSettings.PrivateKey); }
public static void SendNotification(PushSubscriptionInformation subscription, string message) { var pushClient = new WebPushClient(); var vapidDetails = new VapidDetails(VAPIDConfiguration.Subject, VAPIDConfiguration.PublicKey, VAPIDConfiguration.PrivateKey); pushClient.SetVapidDetails(vapidDetails); var pushSubscription = new PushSubscription(subscription.EndPoint, subscription.Keys.p256dh, subscription.Keys.Authentication); pushClient.SendNotification(pushSubscription, JsonConvert.SerializeObject(new RootNotification(message))); }
public WebPushPushNotification(IOptions <WebPushNotificationConfig> options, IStoreRepositoryAccessorProvider pushStoreAccessorProvider, ILogger <WebPushPushNotification> logger) { _logger = logger; _options = options.Value; _pushClient = new WebPushClient(); _pushStoreAccessorProvider = pushStoreAccessorProvider; _pushClient.SetVapidDetails(_options.Subject, _options.PublicKey, _options.PrivateKey); }
private WebPushClient CreateWebPushClient() { var vapidPublicKey = Environment.GetEnvironmentVariable("vapidPublicKey"); var vapidPrivateKey = Environment.GetEnvironmentVariable("vapidPrivateKey"); var vapidDetails = new VapidDetails("mailto:[email protected]", vapidPublicKey, vapidPrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); return(webPushClient); }
public void TestSetVapidDetails() { WebPushClient client = new WebPushClient(); client.SetVapidDetails("mailto:[email protected]", TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); PushSubscription subscription = new PushSubscription(TEST_FCM_ENDPOINT, TEST_PUBLIC_KEY, TEST_PRIVATE_KEY); HttpRequestMessage message = client.GenerateRequestDetails(subscription, "test payload"); string authorizationHeader = message.Headers.GetValues("Authorization").First(); string cryptoHeader = message.Headers.GetValues("Crypto-Key").First(); Assert.True(authorizationHeader.StartsWith("WebPush ")); Assert.True(cryptoHeader.Contains("p256ecdsa")); }
public void TestPassingHttpClient() { var mockHttp = new MockHttpMessageHandler(); mockHttp.When(TestFcmEndpoint).Respond(HttpStatusCode.Created); var client = new WebPushClient(mockHttp.ToHttpClient()); client.SetVapidDetails("mailto:[email protected]", TestPublicKey, TestPrivateKey); var subscription = new PushSubscription(TestFcmEndpoint, TestPublicKey, TestPrivateKey); client.SendNotification(subscription, "123"); }
public async Task <IActionResult> PushNofitication([FromBody] PushNotificationPayload payload) { if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(payload)); } // ToDo: Check if url is valid payload.Url = !string.IsNullOrEmpty(payload.Url) ? payload.Url : $"{Request.Scheme}://{Request.Host.Value}/"; var messageSentCount = 0; var messageFaildCount = 0; var subscriptions = await unitOfWork.Subscribers.GetPushNotificationSubscriptionAsync(); var vapidDetails = new VapidDetails(vapidSettings.Subject, vapidSettings.PublicKey, vapidSettings.PrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); foreach (var subscriber in subscriptions) { try { messageSentCount += 1; webPushClient.SendNotification(subscriber, JsonConvert.SerializeObject(payload), vapidDetails); } catch (Exception ex) when(ex.InnerException is WebPushException) { messageFaildCount += 1; await unitOfWork.Subscribers.RemovePushNotificationSubscriptionAsync(subscriber.Endpoint); } } if (messageFaildCount > 0) { await unitOfWork.SaveAsync(); } return(StatusCode(201, new { subscribersCount = messageSentCount, messageSentCount = messageSentCount - messageFaildCount, messageFaildCount })); }
public async Task <IActionResult> Get() { var vapidProvider = VapidProvider.GetInstance(); var vapidDetails = new VapidDetails( "http://localhost:5000", vapidProvider.PublicKey, vapidProvider.PrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); // send notification var payload = new PushNotificationPayload { Msg = "Test Message to everyone", Icon = "/assets/message.jpg" }; foreach (var sub in _subscriptions) { try { await webPushClient .SendNotificationAsync( sub, JsonConvert.SerializeObject(payload), vapidDetails); } catch (WebPushException exception) { var statusCode = exception.StatusCode; return(new StatusCodeResult((int)statusCode)); } catch (Exception ex) { return(new StatusCodeResult(500)); } } return(new OkResult()); }
public WebPushChannel(ILogStore logStore, IOptions <WebPushOptions> options, IUserNotificationQueue userNotificationQueue, IUserNotificationStore userNotificationStore, IUserStore userStore, IJsonSerializer serializer) { this.serializer = serializer; this.userNotificationQueue = userNotificationQueue; this.userNotificationStore = userNotificationStore; this.userStore = userStore; this.logStore = logStore; webPushClient.SetVapidDetails( options.Value.Subject, options.Value.VapidPublicKey, options.Value.VapidPrivateKey); PublicKey = options.Value.VapidPublicKey; }
public async Task <HttpResponseMessage> SendNotificationAsync(PushSubscription subscription, string payload, WebPushOptions options = null) { options = options ?? WebPushOptions.Defaults; var client = new WebPushClient(); var httpClient = httpClientFactory.CreateClient(); client.SetVapidDetails(vapidAuthenticationOptions.Subject, vapidAuthenticationOptions.PublicKey, vapidAuthenticationOptions.PrivateKey); var request = client.GenerateRequestDetails(new WebPush.PushSubscription() { Auth = subscription.Auth, Endpoint = subscription.Endpoint.ToString(), P256DH = subscription.P256dh }, payload, new Dictionary <string, object>() { { "TTL", (int)options.TimeToLive.TotalSeconds } }); return(await httpClient.SendAsync(request)); }
public async Task <IActionResult> Create([FromBody] PushNotificationSubscription subscription) { var pushSubscription = new PushSubscription(subscription.Endpoint, subscription.Keys.P256dh, subscription.Keys.Auth); var vapidDetails = new VapidDetails( "mailto:[email protected]", "BAvyBimPt4GhPo5qWR-6GhPo6kMlwsmxGgVyRvuRYtEck0Hz4kLOG8lc23p3K_mRH1bgqUU5BdWzxMXb6boVzi4", "WgOFGuwBA8tVzVOQn2M-oFqtgZvlmvJtFLC1VHq-Pho"); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); //TODO; store pushsubscription for later use // send notification var payload = new PushNotificationPayload { notification = new PushNotification { title = "Titulo", body = "Teste de notificacao", icon = "assets/icon-128x128.png" } }; var payloadSerialized = JsonConvert.SerializeObject(payload); try { await webPushClient.SendNotificationAsync(pushSubscription, payloadSerialized, vapidDetails); } catch (WebPushException exception) { var statusCode = exception.StatusCode; return(new StatusCodeResult((int)statusCode)); } return(new OkResult()); }
public static async void PushAllSubs(PushNotificationPayload payload) { var vapidDetails = new VapidDetails("http://localhost:50973/forum", "BAdnuHOxwOFm_GV_NYG1CZOjddlrVfDbKobDFTTxQvgcGBhPI47gkxfEUdtgX2iO_x4PwUkyj-xS7Uke_UmIaqQ", "vrRVfvxyx4kIEYSfansI_eOI4a-HdTCJpa0EVmjLYnE"); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); string temp = JsonConvert.SerializeObject(payload); foreach (PushSubscription sub in subs) { try { await webPushClient.SendNotificationAsync(sub, temp, vapidDetails); } catch (Exception) { } } }
private async void sendPushNotificationClient(string endPoint, string publicKey, string privateKey, string jsonsubdata, string url) { //await when fetching from DB var pushSubscription = new PushSubscription(endPoint.Trim(), publicKey.Trim(), privateKey.Trim()); var webPushClient = new WebPushClient(); //vapid keys generated once on startup of server first time webPushClient.SetVapidDetails(url.Trim(), vapidPublicKEY, vapidPrivateKey); //db query external msg Subscriber sub = GetSubscriberData(publicKey); string title = sub.Header.Trim(); string body = sub.Message.Trim(); JObject payloadjs = new JObject( new JProperty("title", title), new JProperty("body", body) ); webPushClient.SendNotification(pushSubscription, payloadjs.ToString());//,"test",vapidDetails); }
public async Task <IActionResult> Create([FromBody] PushNotificationSubscriptionDto pushNotificationSubscription) { if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } var pushSubscription = new PushSubscription(pushNotificationSubscription.EndPoint, pushNotificationSubscription.Key, pushNotificationSubscription.AuthSecret); var vapidDetails = new VapidDetails(vapidSettings.Subject, vapidSettings.PublicKey, vapidSettings.PrivateKey); var webPushClient = new WebPushClient(); webPushClient.SetVapidDetails(vapidDetails); await unitOfWork.Subscribers.AddPushNotificationSubscriptionAsync(pushNotificationSubscription); await this.unitOfWork.SaveAsync(); var payload = new PushNotificationPayload { Title = "Welcome", Body = "Now you won't miss any post :-)", }; try { await webPushClient.SendNotificationAsync(pushSubscription, JsonConvert.SerializeObject(payload), vapidDetails); } catch (WebPushException exception) { var statusCode = exception.StatusCode; return(StatusCode((int)statusCode, new { message = exception.Message })); } return(StatusCode(201)); }
public PushService(IOptions <VapidKeys> vapidKeys) { _webPushClient.SetVapidDetails("mailto:[email protected]", vapidKeys.Value.Public, vapidKeys.Value.Private); }