public override Task<object> GenerateMessageAsync(PushNotificationItem notificationItem, CancellationToken ct)
 {
     var memorydata = Convert.FromBase64String(notificationItem.MessageContent);
     using (var rs = new MemoryStream(memorydata))
     {
         var sf = new BinaryFormatter();
         return Task.FromResult(sf.Deserialize(rs));
     }
 }
        public override async Task<bool> SendNotificationAsync(PushNotificationItem notificationItem, object message, CancellationToken ct)
        {
            var cfg = (SendGridDeliveryProviderConfiguration)Configuration;
            var sent = false;
            //implicit conversion operator
            MailMessage smsg = message as SerializableMailMessage;
            if (smsg != null)
            {
                try
                {
                    var hView = smsg.AlternateViews.First(v => v.ContentType.MediaType == "text/html");
                    var tView = smsg.AlternateViews.First(v => v.ContentType.MediaType == "text/plain");
                    var sendGridMessage = new SendGridMessage
                    {
                        To = new[]
                        {
                            new MailAddress(notificationItem.Destination.DestinationAddress,
                                notificationItem.Destination.SubscriberName)
                        },
                        From = new MailAddress(cfg.FromAddress, cfg.FromDisplayName),
                        Subject = smsg.Subject,
                        Html = hView.ContentStream.ReadToString(),
                        Text = tView.ContentStream.ReadToString()
                    };

                    if (cfg.EnableClickTracking ?? false)
                    {
                        sendGridMessage.EnableClickTracking();
                    }
                    if (cfg.EnableGravatar ?? false)
                    {
                        sendGridMessage.EnableGravatar();
                    }
                    if (cfg.EnableOpenTracking ?? false)
                    {
                        sendGridMessage.EnableOpenTracking();
                    }
                    if (cfg.SendToSink ?? false)
                    {
                        sendGridMessage.SendToSink();
                    }

                    var transport = new Web(cfg.ApiKey);
                    await transport.DeliverAsync(sendGridMessage);
                    sent = true;
                }
                catch
                {
                    sent = false;
                    //TODO: log this somewhere
                }

            }
            return sent;
        }
        public async Task SendReadyMessageAsync(PushNotificationItem notificationItem, int retryMax, int retryIntv, CancellationToken ct)
        {

            //do the meat
            var message = await GenerateMessageAsync(notificationItem, ct);
            var result = await SendNotificationAsync(notificationItem, message, ct);

            //if we're in a retry case, increment retry count
            if (notificationItem.DeliveryStatus == PushNotificationItemStatus.Retrying)
            {
                notificationItem.RetryCount++;
            }
            if (result)
            {
                //if sent, mark sent and remove schedule
                notificationItem.DeliveryStatus = PushNotificationItemStatus.Sent;
                notificationItem.ScheduledSendDate = null;
            }
            else
            {
                if (notificationItem.RetryCount <= retryMax)
                {
                    //mark for retry, update schedule
                    notificationItem.DeliveryStatus = PushNotificationItemStatus.Retrying;
                    if (notificationItem.ScheduledSendDate != null)
                    {
                        notificationItem.ScheduledSendDate =
                            notificationItem.ScheduledSendDate.Value.AddMinutes(retryIntv ^ notificationItem.RetryCount);
                    }
                }
                else
                {
                    //too many retry attempts, mark fail and clear schedule
                    notificationItem.DeliveryStatus = PushNotificationItemStatus.Failed;
                    notificationItem.ScheduledSendDate = null;
                }
            }
        }
Esempio n. 4
0
        public override Task<bool> SendNotificationAsync(PushNotificationItem notificationItem, object message, CancellationToken ct)
        {
            var cfg = (SmtpDeliveryProviderConfiguration)Configuration;
            var sent = false;
           
            var smsg = message as SerializableMailMessage;
            if (smsg != null)
            {
                try
                {
                    var client = new SmtpClient()
                    {
                        Host = cfg.SmtpServer,
                        Port = cfg.SmtpPort ?? 25,
                        EnableSsl = cfg.EnableSsl ?? false
                    };
                    if (!string.IsNullOrEmpty(cfg.SmtpUserName))
                    {
                        client.Credentials = new NetworkCredential(cfg.SmtpUserName, cfg.SmtpPassword);

                    }
                    smsg.To.Add(new MailAddress(notificationItem.Destination.DestinationAddress, notificationItem.Destination.SubscriberName));
                    smsg.From = new MailAddress(cfg.SmtpFromAddress, cfg.SmtpFromDisplayName);

                    client.Send(smsg);
                    sent = true;
                }
                catch
                {
                    sent = false;
                    //TODO: log this somewhere
                }

            }
            return Task.FromResult(sent);
        }
        public void AddNewEvent(TicketPushNotificationEventInfo eventInfo, ApplicationPushNotificationSetting appSettings, SubscriberNotificationSetting userSetting)
        {
            //no matter what, update to the current message content
            PushNotificationItem.MessageContent = eventInfo.MessageContent;

            if (eventInfo.CancelNotification)
            {
                //remove event
                TicketEvents.Remove(eventInfo.EventId);
                CanceledEvents.Add(eventInfo.EventId);
                if (!TicketEvents.Any())//no events left, scrap entire notification
                {
                    PushNotificationItem.DeliveryStatus    = PushNotificationItemStatus.Canceled;
                    PushNotificationItem.ScheduledSendDate = null;
                }
            }
            else
            {
                //add this event
                TicketEvents.Add(eventInfo.EventId);
                //kick the schedule out if consolidation enabled
                PushNotificationItem.ScheduledSendDate = PushNotificationItem.GetSendDate(appSettings, userSetting);
            }
        }
 public abstract Task<bool> SendNotificationAsync(PushNotificationItem notificationItem, object message, CancellationToken ct);
 public abstract Task<object> GenerateMessageAsync(PushNotificationItem notificationItem, CancellationToken ct);
        private static async Task SendNotificationMessageAsync(TdPushNotificationContext context, PushNotificationItem readyNote, CancellationToken ct)
        {
            var retryMax = context.TicketDeskPushNotificationSettings.RetryAttempts;
            var retryIntv = context.TicketDeskPushNotificationSettings.RetryIntervalMinutes;

            //find a provider for the notification destination type
            var provider = DeliveryProviders.FirstOrDefault(p => p.DestinationType == readyNote.Destination.DestinationType);
            if (provider == null)
            {
                //no provider
                readyNote.DeliveryStatus = PushNotificationItemStatus.NotAvailable;
                readyNote.ScheduledSendDate = null;
            }
            else
            {
                await provider.SendReadyMessageAsync(readyNote, retryMax, retryIntv, ct);
            }
        }