Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for localized alert push notifications (title-loc-key, loc-key, title-loc-args etc) #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion dotAPNS/ApplePush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Dynamic;
using JetBrains.Annotations;
using Newtonsoft.Json;

namespace dotAPNS
{
Expand Down Expand Up @@ -114,14 +115,44 @@ public ApplePush AddMutableContent()
/// <param name="title">Alert title. Can be null.</param>
/// <param name="body">Alert body. <b>Cannot be null.</b></param>
/// <returns></returns>
public ApplePush AddAlert(string title = null, string body = null)
public ApplePush AddAlert([CanBeNull] string title, [NotNull] string body)
{
Alert = new ApplePushAlert(title, body);
if (title == null)
_sendAlertAsText = true;
return this;
}

/// <summary>
/// Add alert to the payload.
/// </summary>
/// <param name="body">Alert body. <b>Cannot be null.</b></param>
/// <returns></returns>
public ApplePush AddAlert([NotNull] string body)
{
return AddAlert(null, body);
}

/// <summary>
/// Add localized alert to the payload.
/// </summary>
/// <param name="titleLocKey"> The key to a title string in the Localizable.strings file for the current localization.
/// The key string can be formatted with %@ and %n$@ specifiers to take the variables specified
/// in the titleLocArgs array. <b>Cannot be null.</b></param>
/// <param name="titleLocArgs"> Variable string values to appear in place of the format specifiers in titleLocKey <b>Can be null.</b></param>
/// <param name="actionLocKey"> If a string is specified, the system displays an alert that includes the Close and View buttons. <b>Can be null.</b></param>
/// <param name="locBody"> A key to an alert-message string in a Localizable.strings file for the current localization
/// (which is set by the user’s language preference). The key string can be formatted with %@ and %n$@
/// specifiers to take the variables specified in the locArgs array. <b>Cannot be null.</b></param>
/// <param name="locArgs">Optional argument list to localized body. <b></b></param>
/// <returns></returns>

public ApplePush AddAlert([CanBeNull] string titleLocKey, [CanBeNull] string[] titleLocArgs, [CanBeNull] string actionLocKey, [NotNull] string locBody, [CanBeNull] params string[]? locArgs)
{
Alert = new ApplePushAlert(titleLocKey, titleLocArgs, actionLocKey, locBody, locArgs);
return this;
}

public ApplePush SetPriority(int priority)
{
if(priority < 0 || priority > 10)
Expand Down Expand Up @@ -160,6 +191,11 @@ public ApplePush AddCategory([NotNull] string category)
return this;
}

/// <summary>
/// Add deviceToken, throws error if token was set
/// </summary>
/// <param name="token">APNS device token</param>
/// <returns></returns>
public ApplePush AddToken([NotNull] string token)
{
if (string.IsNullOrWhiteSpace(token))
Expand All @@ -171,6 +207,21 @@ public ApplePush AddToken([NotNull] string token)
return this;
}

/// <summary>
/// Set a (new) deviceToken
/// </summary>
/// <param name="token">APNS device token</param>
/// <returns></returns>
public ApplePush SetToken([NotNull] string token)
{
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentException("Value cannot be null or whitespace.", nameof(token));
if (Type == ApplePushType.Voip)
throw new InvalidOperationException($"Please use SetVoipToken() when sending {nameof(ApplePushType.Voip)} pushes.");
Token = token;
return this;
}

public ApplePush AddVoipToken([NotNull] string voipToken)
{
if (string.IsNullOrWhiteSpace(voipToken))
Expand Down Expand Up @@ -231,6 +282,8 @@ public object GeneratePayload()
object alert;
if (_sendAlertAsText)
alert = Alert.Body;
else if (Alert.Localized)
alert = Alert;
else
alert = new { title = Alert.Title, body = Alert.Body };
payload.aps.alert = alert;
Expand Down Expand Up @@ -262,17 +315,50 @@ public object GeneratePayload()
}
}

[JsonObject(MemberSerialization.OptIn)]
public class ApplePushAlert
{
[JsonProperty("title")]
public string Title { get; }

[JsonProperty("body")]
public string Body { get; }

[JsonProperty("title-loc-key")]
public string LocTitle { get; }

[JsonProperty("title-loc-args")]
public string[] TitleArgs { get; }

[JsonProperty("action-loc-key")]
public string ActionLocKey { get; }

[JsonProperty("loc-key")]
public string LocBody { get; }

[JsonProperty("loc-args")]
public string[] BodyArgs { get; }

public bool Localized { get; }

public ApplePushAlert([CanBeNull] string title, [NotNull] string body)
{
Localized = false;
Title = title;
Body = body ?? throw new ArgumentNullException(nameof(body));
}

public ApplePushAlert([CanBeNull] string titleLocKey, [CanBeNull] string[] titleLocArgs, [CanBeNull] string actionLocKey, [NotNull] string locKey, [CanBeNull] params string[]? locArgs)
{
Localized = true;
LocTitle = titleLocKey;
TitleArgs = titleLocArgs;
ActionLocKey = actionLocKey;
LocBody = locKey ?? throw new ArgumentNullException(nameof(locKey));
BodyArgs = (locArgs.Length == 0) ? null: locArgs;
}


}

}
3 changes: 1 addition & 2 deletions dotAPNS/JsonContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ public class JsonContent : StringContent
{
const string JsonMediaType = "application/json";

public JsonContent(object obj) : this(obj is string str ? str : JsonConvert.SerializeObject(obj))
public JsonContent(object obj) : this(obj is string str ? str : JsonConvert.SerializeObject(obj, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }))
{

}

JsonContent(string content) : base(content, Encoding.UTF8, JsonMediaType)
Expand Down