Permalink
Browse files

New WP8 Tile Templates, and XML to Linq'ified

Also Fixes #7 finally (Manually merged changes)
  • Loading branch information...
1 parent 1467866 commit 5b79c1e04073ebafc98d44f475b61d0069225971 @Redth committed Jan 24, 2013
View
BIN NuGet/PushSharp.1.1.1.0.nupkg
Binary file not shown.
View
4 PushSharp.Common/AssemblyVersionInfo.cs
@@ -12,5 +12,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.1.0.0")]
-[assembly: AssemblyFileVersion("1.1.0.0")]
+[assembly: AssemblyVersion("1.1.1.0")]
+[assembly: AssemblyFileVersion("1.1.1.0")]
View
105 PushSharp.Windows/WindowsNotification.cs
@@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using System.Xml.Linq;
namespace PushSharp.Windows
{
@@ -11,15 +12,13 @@ public abstract class WindowsNotification : Common.Notification
protected WindowsNotification()
{
this.Platform = Common.PlatformType.Windows;
-
}
public string ChannelUri { get; set; }
public bool? RequestForStatus { get; set; }
public int? TimeToLive { get; set; }
-
public abstract string PayloadToString();
public abstract WindowsNotificationType Type { get; }
@@ -28,6 +27,40 @@ protected string XmlEncode(string text)
{
return System.Security.SecurityElement.Escape(text);
}
+
+ protected string GeneratePayload(XElement rootElement, string template, Dictionary<string, string> images, List<string> texts)
+ {
+ var visual = new XElement("visual");
+ var binding = new XElement("binding", new XAttribute("template", template.ToString()));
+
+ int idOn = 1;
+
+ foreach (var imgSrc in images.Keys)
+ {
+ var alt = images[imgSrc];
+
+ var image = new XElement("image", new XAttribute("id", idOn), new XAttribute("src", XmlEncode(imgSrc)));
+
+ if (!string.IsNullOrEmpty(alt))
+ image.Add(new XAttribute("alt", XmlEncode(alt)));
+
+ binding.Add(image);
+
+ idOn++;
+ }
+
+ idOn = 1;
+
+ foreach (var text in texts)
+ {
+ binding.Add(new XElement("text", new XAttribute("id", idOn), XmlEncode(text)));
+ idOn++;
+ }
+
+ visual.Add(binding);
+ rootElement.Add(visual);
+ return rootElement.ToString();
+ }
}
public class WindowsTileNotification : WindowsNotification
@@ -54,39 +87,7 @@ public override WindowsNotificationType Type
public override string PayloadToString()
{
- var xml = new StringBuilder();
-
- xml.Append("<tile>");
- xml.Append("<visual>");
- xml.AppendFormat("<binding template=\"{0}\">", this.TileTemplate.ToString());
-
- int idOn = 1;
-
- foreach (var imgSrc in Images.Keys)
- {
- var alt = Images[imgSrc];
-
- if (!string.IsNullOrEmpty(alt))
- xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
- else
- xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
-
- idOn++;
- }
-
- idOn = 1;
-
- foreach (var text in Texts)
- {
- xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
- idOn++;
- }
-
- xml.Append("</binding>");
- xml.Append("</visual>");
- xml.Append("</tile>");
-
- return xml.ToString();
+ return this.GeneratePayload(new XElement("tile"), this.TileTemplate.ToString(), Images, Texts);
}
}
@@ -111,39 +112,7 @@ public override WindowsNotificationType Type
public override string PayloadToString()
{
- var xml = new StringBuilder();
-
- xml.Append("<toast>");
- xml.Append("<visual>");
- xml.AppendFormat("<binding template=\"{0}\">", this.TextTemplate.ToString());
-
- int idOn = 1;
-
- foreach (var imgSrc in Images.Keys)
- {
- var alt = Images[imgSrc];
-
- if (!string.IsNullOrEmpty(alt))
- xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
- else
- xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
-
- idOn++;
- }
-
- idOn = 1;
-
- foreach (var text in Texts)
- {
- xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
- idOn++;
- }
-
- xml.Append("</binding>");
- xml.Append("</visual>");
- xml.Append("</toast>");
-
- return xml.ToString();
+ return this.GeneratePayload(new XElement("toast"), this.TextTemplate.ToString(), Images, Texts);
}
}
View
111 PushSharp.WindowsPhone/WindowsPhoneNotification.cs
@@ -96,11 +96,6 @@ protected string XmlEncode(string text)
public class WindowsPhoneToastNotification : WindowsPhoneNotification
{
- public WindowsPhoneToastNotification()
- : base()
- {
- }
-
public string Text1 { get; set; }
public string Text2 { get; set; }
@@ -110,23 +105,23 @@ public WindowsPhoneToastNotification()
public override string PayloadToString()
{
- var sb = new StringBuilder();
+ XNamespace wp = "WPNotification";
+ var notification = new XElement(wp + "Notification", new XAttribute(XNamespace.Xmlns + "wp", "WPNotification"));
- sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
- sb.AppendLine("<wp:Toast>");
+ var toast = new XElement(wp + "Toast");
if (!string.IsNullOrEmpty(Text1))
- sb.AppendLine("<wp:Text1>" + XmlEncode(Text1) + "</wp:Text1>");
+ toast.Add(new XElement(wp + "Text1", XmlEncode(Text1)));
if (!string.IsNullOrEmpty(Text2))
- sb.AppendLine("<wp:Text2>" + XmlEncode(Text2) + "</wp:Text2>");
-
+ toast.Add(new XElement(wp + "Text2", XmlEncode(Text2)));
+
+
if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
{
if (!string.IsNullOrEmpty(NavigatePath) || (Parameters != null && Parameters.Count > 0))
{
- sb.Append("<wp:Param>");
+ var sb = new StringBuilder();
if (!string.IsNullOrEmpty(NavigatePath))
sb.Append(XmlEncode("/" + NavigatePath.TrimStart('/')));
@@ -139,25 +134,20 @@ public override string PayloadToString()
sb.Append(XmlEncode(key + "=" + Parameters[key].ToString()) + "&amp;");
}
- sb.AppendLine("</wp:Param>");
+ var paramValue = sb.ToString();
+
+ if (!string.IsNullOrEmpty(paramValue))
+ toast.Add(new XElement(wp + "Param", paramValue));
}
}
- sb.AppendLine("</wp:Toast>");
- sb.AppendLine("</wp:Notification>");
-
- return sb.ToString();
+ notification.Add(toast);
+ return notification.ToString();
}
}
public class WindowsPhoneRawNotification : WindowsPhoneNotification
{
- public WindowsPhoneRawNotification()
- : base()
- {
- }
-
-
public string Raw { get; set; }
public override string PayloadToString()
@@ -168,11 +158,6 @@ public override string PayloadToString()
public class WindowsPhoneTileNotification : WindowsPhoneNotification
{
- public WindowsPhoneTileNotification()
- : base()
- {
- }
-
public string TileId { get; set; } //Secondary tile id, leave blank for application tile
public string BackgroundImage { get; set; }
@@ -194,56 +179,48 @@ public WindowsPhoneTileNotification()
public override string PayloadToString()
{
- var sb = new StringBuilder();
-
- sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
- sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
-
- sb.Append("<wp:Tile");
-
- if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
- {
- if (!string.IsNullOrEmpty(this.TileId))
- sb.Append(" Id=\"" + XmlEncode(this.TileId) + "\"");
- }
+ XNamespace wp = "WPNotification";
+ var notification = new XElement(wp + "Notification", new XAttribute(XNamespace.Xmlns + "wp", "WPNotification"));
- sb.AppendLine(">");
+ var tile = new XElement(wp + "Tile");
+
+ if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven && !string.IsNullOrEmpty(this.TileId))
+ tile.Add(new XAttribute("Id", XmlEncode(this.TileId)));
if (!string.IsNullOrEmpty(BackgroundImage))
- sb.AppendLine("<wp:BackgroundImage>" + XmlEncode(this.BackgroundImage) + "</wp:BackgroundImage>");
-
+ tile.Add(new XElement(wp + "BackgroundImage", XmlEncode(BackgroundImage)));
+
if (ClearCount)
- sb.AppendLine("<wp:Count Action=\"Clear\"></wp:Count>");
+ tile.Add(new XElement(wp + "Count", new XAttribute("Action", "Clear")));
else if (Count.HasValue)
- sb.AppendLine("<wp:Count>" + Count.ToString() + "</wp:Count>");
+ tile.Add(new XElement(wp + "Count", XmlEncode(Count.ToString())));
if (ClearTitle)
- sb.AppendLine("<wp:Title Action=\"Clear\"></wp:Title>");
+ tile.Add(new XElement(wp + "Title", new XAttribute("Action", "Clear")));
else if (!string.IsNullOrEmpty(Title))
- sb.AppendLine("<wp:Title>" + XmlEncode(Title) + "</wp:Title>");
-
- if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
+ tile.Add(new XElement(wp + "Title", XmlEncode(Title)));
+
+ if (OSVersion > WindowsPhoneDeviceOSVersion.Seven)
{
- if (ClearBackBackgroundImage)
- sb.AppendLine("<wp:BackBackgroundImage Action=\"Clear\"></wp:BackBackgroundImage>");
- else if (!string.IsNullOrEmpty(BackBackgroundImage))
- sb.AppendLine("<wp:BackBackgroundImage>" + XmlEncode(BackBackgroundImage) + "</wp:BackBackgroundImage>");
-
if (ClearBackTitle)
- sb.AppendLine("<wp:BackTitle Action=\"Clear\"></wp:BackTitle>");
- else if (!string.IsNullOrEmpty(BackTitle))
- sb.AppendLine("<wp:BackTitle>" + XmlEncode(BackTitle) + "</wp:BackTitle>");
+ tile.Add(new XElement(wp + "BackTitle", new XAttribute("Action", "Clear")));
+ else if (!string.IsNullOrEmpty(Title))
+ tile.Add(new XElement(wp + "BackTitle", XmlEncode(BackTitle)));
+
+ if (ClearBackBackgroundImage)
+ tile.Add(new XElement(wp + "BackBackgroundImage", new XAttribute("Action", "Clear")));
+ else if (!string.IsNullOrEmpty(Title))
+ tile.Add(new XElement(wp + "BackBackgroundImage", XmlEncode(BackBackgroundImage)));
if (ClearBackContent)
- sb.AppendLine("<wp:BackContent Action=\"Clear\"></wp:BackContent>");
- else if (!string.IsNullOrEmpty(BackContent))
- sb.AppendLine("<wp:BackContent>" + XmlEncode(BackContent) + "</wp:BackContent>");
+ tile.Add(new XElement(wp + "BackContent", new XAttribute("Action", "Clear")));
+ else if (!string.IsNullOrEmpty(Title))
+ tile.Add(new XElement(wp + "BackContent", XmlEncode(BackContent)));
+
}
- sb.AppendLine("</wp:Tile>");
- sb.AppendLine("</wp:Notification>");
-
- return sb.ToString();
+ notification.Add(tile);
+ return notification.ToString();
}
}
@@ -473,8 +450,7 @@ public override string PayloadToString()
tile.Add(new XElement(wp + "Count", new XAttribute("Action", "Clear")));
else if (Count.HasValue)
tile.Add(new XElement(wp + "Count", XmlEncode(Count.Value.ToString())));
-
-
+
if (ClearCycleImage1)
tile.Add(new XElement(wp + "CycleImage1", new XAttribute("Action", "Clear")));
else if (!string.IsNullOrEmpty(Title))
@@ -520,7 +496,6 @@ public override string PayloadToString()
else if (!string.IsNullOrEmpty(Title))
tile.Add(new XElement(wp + "CycleImage9", XmlEncode(CycleImage9)));
-
notification.Add(tile);
return notification.ToString();
View
2 PushSharp.WindowsPhone/WindowsPhonePushService.cs
@@ -9,7 +9,7 @@ namespace PushSharp.WindowsPhone
public class WindowsPhonePushService : PushServiceBase<WindowsPhonePushChannelSettings>
{
public WindowsPhonePushService(WindowsPhonePushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
- : base(channelSettings, serviceSettings)
+ : base(channelSettings ?? new WindowsPhonePushChannelSettings(), serviceSettings)
{
}

0 comments on commit 5b79c1e

Please sign in to comment.