public static string?Body(this BaseUserNotification notification, bool asHtml = false) { var formatting = notification.Formatting; var body = formatting.Body; if (asHtml && !string.IsNullOrWhiteSpace(formatting.LinkText) && !string.IsNullOrWhiteSpace(formatting.LinkUrl)) { if (body?.Length > 0) { return($"{body} <a href=\"{formatting.LinkUrl}\">{formatting.LinkText}</a>"); } else { return($"<a href=\"{formatting.LinkUrl}\">{formatting.LinkText}</a>"); } } if (!string.IsNullOrWhiteSpace(formatting.LinkUrl)) { if (body?.Length > 0) { return($"{body} {formatting.LinkUrl}"); } else { return(formatting.LinkUrl); } } return(body); }
public static string BuildTrackingLink(BaseUserNotification notification, string emailAddress) { var trackingUrl = notification.ComputeTrackSeenUrl(Providers.Email, emailAddress); var trackingLink = $"<img height=\"0\" width=\"0\" style=\"width: 0px; height: 0px; position: absolute; visibility: hidden;\" src=\"{trackingUrl}\" />"; return(trackingLink); }
public string Format(MessagingTemplate?template, BaseUserNotification notification) { Guard.NotNull(notification); if (template == null) { return(notification.Formatting.Subject); } var formattingProperties = new Dictionary <string, string?> { ["notification.body"] = notification.Body(), ["notification.confirmText"] = notification.ConfirmText(), ["notification.confirmUrl"] = notification.ConfirmUrl(), ["notification.imageLarge"] = notification.ImageLarge(imageFormatter, "MessagingLarge"), ["notification.imageSmall"] = notification.ImageSmall(imageFormatter, "MessagingSmall"), ["notification.subject"] = notification.Subject() }; var properties = notification.Properties; if (properties != null) { foreach (var(key, value) in properties) { formattingProperties[$"notification.custom.{key}"] = value; } } return(template.Text.Format(formattingProperties)); }
public async Task SendAsync(BaseUserNotification userNotification, MobilePushOptions options, CancellationToken ct) { if (!ShouldSend(userNotification.Silent, options.DeviceType)) { return; } try { var message = userNotification.ToFirebaseMessage(options.DeviceToken, options.Wakeup, options.IsConfirmed); // Try a few attempts to get a non-disposed messaging service. for (var i = 1; i <= Attempts; i++) { try { await wrapper().Messaging.SendAsync(message, ct); break; } catch (ObjectDisposedException) { if (i == Attempts) { throw; } } } } catch (FirebaseMessagingException ex) when(ex.MessagingErrorCode == MessagingErrorCode.Unregistered) { throw new MobilePushTokenExpiredException(); } }
public static string Subject(this BaseUserNotification notification, bool asHtml = false) { var formatting = notification.Formatting; var subject = formatting.Subject !; if (asHtml && !string.IsNullOrWhiteSpace(formatting.LinkUrl)) { subject = $"<a href=\"{formatting.LinkUrl}\" target=\"_blank\" rel=\"noopener\">{subject}</a>"; } return(subject); }
public static IEnumerable <ActivityLink> Links(this BaseUserNotification notification) { if (notification.UserEventActivity != default) { yield return(new ActivityLink(notification.UserEventActivity)); } if (notification.EventActivity != default) { yield return(new ActivityLink(notification.EventActivity)); } if (notification.NotificationActivity != default) { yield return(new ActivityLink(notification.NotificationActivity)); } }
public static string ImageLarge(this BaseUserNotification notification, IImageFormatter imageFormatter, string preset) { var formatting = notification.Formatting; return(imageFormatter.Format(formatting.ImageLarge, preset)); }
public static string?ConfirmUrl(this BaseUserNotification notification) { return(notification.ConfirmUrl); }
public static string?ConfirmText(this BaseUserNotification notification) { return(notification.Formatting.ConfirmText); }
public static Message ToFirebaseMessage(this BaseUserNotification notification, string token, bool wakeup, bool isConfirmed) { var message = new Message { Token = token }; if (wakeup) { message.Data = new Dictionary <string, string>() .WithNonEmpty("id", notification.Id.ToString()); return(message); } var formatting = notification.Formatting; message.Data = new Dictionary <string, string>() .WithNonEmpty("id", notification.Id.ToString()) .WithNonEmpty("confirmText", formatting.ConfirmText) .WithNonEmpty("confirmUrl", notification.ComputeConfirmUrl(Providers.MobilePush, token)) .WithNonEmpty("isConfirmed", isConfirmed.ToString()) .WithNonEmpty("imageLarge", formatting.ImageLarge) .WithNonEmpty("imageSmall", formatting.ImageSmall) .WithNonEmpty("linkText", formatting.LinkText) .WithNonEmpty("linkUrl", formatting.LinkUrl) .WithNonEmpty("silent", notification.Silent.ToString()) .WithNonEmpty("trackDeliveredUrl", notification.ComputeTrackDeliveredUrl(Providers.MobilePush, token)) .WithNonEmpty("trackSeenUrl", notification.ComputeTrackSeenUrl(Providers.MobilePush, token)) .WithNonEmpty("trackingUrl", notification.ComputeTrackSeenUrl(Providers.MobilePush, token)) .WithNonEmpty("data", notification.Data); var androidData = new Dictionary <string, string>() .WithNonEmpty("subject", formatting.Subject) .WithNonEmpty("body", formatting.Body); var androidConfig = new AndroidConfig { Data = androidData, Priority = Priority.High }; var apsAlert = new ApsAlert { Title = formatting.Subject }; if (!string.IsNullOrWhiteSpace(formatting.Body)) { apsAlert.Body = formatting.Body; } var apnsHeaders = new Dictionary <string, string> { ["apns-collapse-id"] = notification.Id.ToString(), ["apns-push-type"] = "alert" }; if (notification.TimeToLiveInSeconds is int timeToLive) { androidConfig.TimeToLive = TimeSpan.FromSeconds(timeToLive); var unixTimeSeconds = DateTimeOffset.UtcNow.AddSeconds(timeToLive).ToUnixTimeSeconds().ToString(CultureInfo.InvariantCulture); apnsHeaders["apns-expiration"] = timeToLive == 0 ? "0" : unixTimeSeconds; } var apnsConfig = new ApnsConfig { Headers = apnsHeaders, Aps = new Aps { Alert = apsAlert, MutableContent = true } }; message.Android = androidConfig; message.Apns = apnsConfig; return(message); }