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> 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 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()); }
private CommandNotification PushNotifications(IEnumerable <Visit> visits) { foreach (var visit in visits) { var pushCampaignListResult = _campaignSearch.FindMessagesForPlace(visit.PlaceId); if (pushCampaignListResult.IsInvalid) { return(new CommandNotification(pushCampaignListResult)); } foreach (var pushCampaign in pushCampaignListResult.Object) { var providerResult = _pushNotificationProviderFactory.Create(pushCampaign.Provider); if (providerResult.IsInvalid) { return(new CommandNotification(providerResult)); } var provider = providerResult.Object; var payload = new PushNotificationPayload() { DeviceId = visit.DeviceId, Message = pushCampaign.Message, VisitId = visit.Id }; provider.PushNotification(payload); } } return(new CommandNotification()); }
/// <returns></returns> private async Task <ProcessResult <DeliveryModel> > DeliveryNotificationsAsync(long deliveryKey) { ProcessResult <DeliveryModel> retVal; try { DeliveryModel dmodel = await _deliveryQueries.GetDeliveryAsync(deliveryKey); var pushNotification = new PushNotification(dmodel.Notification.Title, dmodel.Notification.Message, dmodel.Notification.IconUrl, dmodel.Notification.Url, dmodel.Notification.ImageUrl); PushNotificationPayload payload = new PushNotificationPayload(pushNotification); ProcessResult result = await _pushService.SendAsync(dmodel.Subscription.Endpoint, dmodel.Subscription.PublicKey, dmodel.Subscription.Token, payload); retVal = new ProcessResult <DeliveryModel>(dmodel); retVal.PopulateErrorFromResult(result); } catch (Exception ex) { retVal = new ProcessResult <DeliveryModel>(); retVal.SetErrorInfo($"DeliveryNotification Error: {ex.Message}"); } return(retVal); }
//And then, calling Push() method to invoke it public void Push(PushNotificationPayload payload) { string payloadStr = payload.PushPayload(); string deviceToken = payload.deviceToken; MemoryStream memoryStream = new MemoryStream(); BinaryWriter writer = new BinaryWriter(memoryStream); writer.Write((byte)0); //The command writer.Write((byte)0); //The first byte of deviceId length (Big-endian first byte) writer.Write((byte)32); //The deviceId length (Big-endian second type) //Method of DataWithDeviceToken() , see source code in this repo. byte[] deviceTokenBytes = DataWithDeviceToken(deviceToken.ToUpper()); writer.Write(deviceTokenBytes); writer.Write((byte)0); //The first byte of payload length (Big-endian first byte) writer.Write((byte)payloadStr.Length); //payload length (Big-endian second byte) byte[] bytes = Encoding.UTF8.GetBytes(payloadStr); writer.Write(bytes); writer.Flush(); _sslStream.Write(memoryStream.ToArray()); _sslStream.Flush(); Thread.Sleep(3000); //Method of ReadMessage() , see source code in this repo. string result = ReadMessage(_sslStream); Console.WriteLine("server said: " + result); _sslStream.Close(); }
public async Task <CommandNotification> PushCampaignAsync(Visit visit) { var readCacheResult = await _setCache.GetAllAsync(visit.PlaceId.ToString()); if (readCacheResult.IsInvalid) { return(new CommandNotification(readCacheResult)); } var pushCampaigns = readCacheResult.Object .Select(raw => JsonConvert.DeserializeObject <PushCampaign>(raw)); if (!pushCampaigns.Any()) { pushCampaigns = new List <PushCampaign>() { new PushCampaign() { Message = string.Empty, Provider = string.Empty } }; } ObjectWithNotification <IPushNotificationProvider> providerResult; IPushNotificationProvider provider; PushNotificationPayload payload; CommandNotification pushNotification; foreach (var pushCampaign in pushCampaigns) { providerResult = _pushNotificationProviderFactory.Create(pushCampaign.Provider); if (providerResult.IsInvalid) { return(new CommandNotification(providerResult)); } provider = providerResult.Object; payload = new PushNotificationPayload() { DeviceId = visit.DeviceId, Message = pushCampaign.Message, VisitId = visit.Id }; pushNotification = provider.PushNotification(payload); if (pushNotification.IsInvalid) { return(new CommandNotification(pushNotification)); } } return(new CommandNotification()); }
public CommandNotification PushNotification(PushNotificationPayload payload) { var preffix = PushNotificationProviderHelper.GetVisitDescription(payload); _textWriter.WriteLine(preffix); _textWriter.WriteLine("===> No campaign with matching target"); _textWriter.WriteLine(); return(new CommandNotification()); }
public RemoteClient() { X509Certificate2 cert = new X509Certificate2(CertificatePath, CertificatePass); certificate = new X509CertificateCollection(); certificate.Add(cert); _payload = new PushNotificationPayload(); _payload.deviceToken = "dc67b56c eb5dd9f9 782c37fd cfdcca87 3b7bc77c 3b090ac4 c538e007 a2f23a24"; _payload.badge = 56789; _payload.sound = "default"; _payload.message = "This message was pushed by C# platform."; }
static void Main(string[] args) { string filepath = "C://AppPushCertificates.p12"; string pwd = "your certificate passwords"; PushNotification pushNotification = new PushNotification(PushNotificationType.Distribution, filepath, pwd); PushNotificationPayload payload = new PushNotificationPayload(); payload.deviceToken = "dc67b56c eb5dd9f9 782c37fd cfdcca87 3b7bc77c 3b090ac4 c538e007 a2f23a24"; payload.badge = 56789; payload.sound = "default"; payload.message = "This message was pushed by C# platform."; pushNotification.Push(payload); }
public CommandNotification PushNotification(PushNotificationPayload payload) { var visitDescription = PushNotificationProviderHelper.GetVisitDescription(payload); _textWriter.WriteLine(visitDescription); var deviceDescription = PushNotificationProviderHelper.GetDeviceDescription(payload); _textWriter.WriteLine(deviceDescription); _textWriter.WriteLine($"===> Mixpanel logging: Olar! {payload.Message}"); _textWriter.WriteLine(); return(new CommandNotification()); }
public CommandNotification PushNotification(PushNotificationPayload payload) { var visitDescription = PushNotificationProviderHelper.GetVisitDescription(payload); _textWriter.WriteLine(visitDescription); var deviceDescription = PushNotificationProviderHelper.GetDeviceDescription(payload); _textWriter.WriteLine(deviceDescription); _textWriter.WriteLine($"===> Localytics logging: {{ \"message\": \"{payload.Message}\", device_id: \"{payload.DeviceId}\" }}"); _textWriter.WriteLine(); return(new CommandNotification()); }
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 <ForumPost> Post([FromBody] ForumPost post, int key) { if (!Sessions.IsValidSession(key)) { return(null); } try { using (SqlConnection connection = new SqlConnection(CON_STRING)) { await connection.OpenAsync(); const string commandString = "INSERT INTO [dbo].[message](message, sendDate, username) VALUES ( @text, GETDATE(), @username);"; using (SqlCommand command = new SqlCommand(commandString, connection)) { command.Parameters.AddWithValue("username", post.Username); command.Parameters.AddWithValue("text", post.Text); int lines = await command.ExecuteNonQueryAsync(); if (lines != 0) { var payload = new PushNotificationPayload { Msg = post.Text, Icon = "C:/Temp/icon192x192.png", }; ForumPostSubscriptions.PushAllSubs(payload); return(post); } return(null); } } } catch (Exception) { return(null); } }
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) { } } }
public async Task <(bool, int)> AddNewSubscriber(PushNotificationSubscription newSubscription, string baseUrl) { var pushSub = new PushSubscription(newSubscription.Endpoint, newSubscription.Key, newSubscription.AuthSecret); await SavePushSubscription(pushSub); var payload = new PushNotificationPayload { Msg = "Thank you for subscribing", Icon = GetIconUrl(baseUrl) }; try { await _pushClient.SendNotificationAsync(pushSub, JsonConvert.SerializeObject(payload)); return(true, 200); } catch (WebPushException wpe) { return(false, (int)wpe.StatusCode); } }
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 async Task SendNotificationToSubscribers(string message, string baseUrl) { var notificationPayload = new PushNotificationPayload { Msg = message, Icon = GetIconUrl(baseUrl) }; var payloadJson = JsonConvert.SerializeObject(notificationPayload); var subscribers = GetSubscribers(); foreach (var subscriber in subscribers) { try { await _pushClient.SendNotificationAsync(subscriber, payloadJson); } catch (WebPushException wpe) { //add logging } } }
public static string GetDeviceDescription(PushNotificationPayload pushNotificationPayload) { return($"===> Device ID: \"{pushNotificationPayload.DeviceId}\""); }
public static string GetVisitDescription(PushNotificationPayload pushNotificationPayload) { return($"=> Push sent regarding visit {pushNotificationPayload.VisitId}"); }