Skip to content

Commit 5b79c1e

Browse files
committed
New WP8 Tile Templates, and XML to Linq'ified
Also Fixes Redth#7 finally (Manually merged changes)
1 parent 1467866 commit 5b79c1e

File tree

5 files changed

+83
-139
lines changed

5 files changed

+83
-139
lines changed

NuGet/PushSharp.1.1.1.0.nupkg

136 KB
Binary file not shown.

PushSharp.Common/AssemblyVersionInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
// You can specify all the values or you can default the Build and Revision Numbers
1313
// by using the '*' as shown below:
1414
// [assembly: AssemblyVersion("1.0.*")]
15-
[assembly: AssemblyVersion("1.1.0.0")]
16-
[assembly: AssemblyFileVersion("1.1.0.0")]
15+
[assembly: AssemblyVersion("1.1.1.0")]
16+
[assembly: AssemblyFileVersion("1.1.1.0")]

PushSharp.Windows/WindowsNotification.cs

+37-68
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using System.Xml.Linq;
67

78
namespace PushSharp.Windows
89
{
@@ -11,15 +12,13 @@ public abstract class WindowsNotification : Common.Notification
1112
protected WindowsNotification()
1213
{
1314
this.Platform = Common.PlatformType.Windows;
14-
1515
}
1616

1717
public string ChannelUri { get; set; }
1818

1919
public bool? RequestForStatus { get; set; }
2020
public int? TimeToLive { get; set; }
2121

22-
2322
public abstract string PayloadToString();
2423

2524
public abstract WindowsNotificationType Type { get; }
@@ -28,6 +27,40 @@ protected string XmlEncode(string text)
2827
{
2928
return System.Security.SecurityElement.Escape(text);
3029
}
30+
31+
protected string GeneratePayload(XElement rootElement, string template, Dictionary<string, string> images, List<string> texts)
32+
{
33+
var visual = new XElement("visual");
34+
var binding = new XElement("binding", new XAttribute("template", template.ToString()));
35+
36+
int idOn = 1;
37+
38+
foreach (var imgSrc in images.Keys)
39+
{
40+
var alt = images[imgSrc];
41+
42+
var image = new XElement("image", new XAttribute("id", idOn), new XAttribute("src", XmlEncode(imgSrc)));
43+
44+
if (!string.IsNullOrEmpty(alt))
45+
image.Add(new XAttribute("alt", XmlEncode(alt)));
46+
47+
binding.Add(image);
48+
49+
idOn++;
50+
}
51+
52+
idOn = 1;
53+
54+
foreach (var text in texts)
55+
{
56+
binding.Add(new XElement("text", new XAttribute("id", idOn), XmlEncode(text)));
57+
idOn++;
58+
}
59+
60+
visual.Add(binding);
61+
rootElement.Add(visual);
62+
return rootElement.ToString();
63+
}
3164
}
3265

3366
public class WindowsTileNotification : WindowsNotification
@@ -54,39 +87,7 @@ public override WindowsNotificationType Type
5487

5588
public override string PayloadToString()
5689
{
57-
var xml = new StringBuilder();
58-
59-
xml.Append("<tile>");
60-
xml.Append("<visual>");
61-
xml.AppendFormat("<binding template=\"{0}\">", this.TileTemplate.ToString());
62-
63-
int idOn = 1;
64-
65-
foreach (var imgSrc in Images.Keys)
66-
{
67-
var alt = Images[imgSrc];
68-
69-
if (!string.IsNullOrEmpty(alt))
70-
xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
71-
else
72-
xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
73-
74-
idOn++;
75-
}
76-
77-
idOn = 1;
78-
79-
foreach (var text in Texts)
80-
{
81-
xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
82-
idOn++;
83-
}
84-
85-
xml.Append("</binding>");
86-
xml.Append("</visual>");
87-
xml.Append("</tile>");
88-
89-
return xml.ToString();
90+
return this.GeneratePayload(new XElement("tile"), this.TileTemplate.ToString(), Images, Texts);
9091
}
9192
}
9293

@@ -111,39 +112,7 @@ public override WindowsNotificationType Type
111112

112113
public override string PayloadToString()
113114
{
114-
var xml = new StringBuilder();
115-
116-
xml.Append("<toast>");
117-
xml.Append("<visual>");
118-
xml.AppendFormat("<binding template=\"{0}\">", this.TextTemplate.ToString());
119-
120-
int idOn = 1;
121-
122-
foreach (var imgSrc in Images.Keys)
123-
{
124-
var alt = Images[imgSrc];
125-
126-
if (!string.IsNullOrEmpty(alt))
127-
xml.AppendFormat("<image id=\"{0}\" src=\"{1}\" alt=\"{2}\"/>", idOn, XmlEncode(imgSrc), XmlEncode(alt));
128-
else
129-
xml.AppendFormat("<image id=\"{0}\" src=\"{1}\"/>", idOn, XmlEncode(imgSrc));
130-
131-
idOn++;
132-
}
133-
134-
idOn = 1;
135-
136-
foreach (var text in Texts)
137-
{
138-
xml.AppendFormat("<text id=\"{0}\">{1}</text>", idOn, XmlEncode(text));
139-
idOn++;
140-
}
141-
142-
xml.Append("</binding>");
143-
xml.Append("</visual>");
144-
xml.Append("</toast>");
145-
146-
return xml.ToString();
115+
return this.GeneratePayload(new XElement("toast"), this.TextTemplate.ToString(), Images, Texts);
147116
}
148117
}
149118

PushSharp.WindowsPhone/WindowsPhoneNotification.cs

+43-68
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ protected string XmlEncode(string text)
9696

9797
public class WindowsPhoneToastNotification : WindowsPhoneNotification
9898
{
99-
public WindowsPhoneToastNotification()
100-
: base()
101-
{
102-
}
103-
10499
public string Text1 { get; set; }
105100
public string Text2 { get; set; }
106101

@@ -110,23 +105,23 @@ public WindowsPhoneToastNotification()
110105

111106
public override string PayloadToString()
112107
{
113-
var sb = new StringBuilder();
108+
XNamespace wp = "WPNotification";
109+
var notification = new XElement(wp + "Notification", new XAttribute(XNamespace.Xmlns + "wp", "WPNotification"));
114110

115-
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
116-
sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
117-
sb.AppendLine("<wp:Toast>");
111+
var toast = new XElement(wp + "Toast");
118112

119113
if (!string.IsNullOrEmpty(Text1))
120-
sb.AppendLine("<wp:Text1>" + XmlEncode(Text1) + "</wp:Text1>");
114+
toast.Add(new XElement(wp + "Text1", XmlEncode(Text1)));
121115

122116
if (!string.IsNullOrEmpty(Text2))
123-
sb.AppendLine("<wp:Text2>" + XmlEncode(Text2) + "</wp:Text2>");
124-
117+
toast.Add(new XElement(wp + "Text2", XmlEncode(Text2)));
118+
119+
125120
if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
126121
{
127122
if (!string.IsNullOrEmpty(NavigatePath) || (Parameters != null && Parameters.Count > 0))
128123
{
129-
sb.Append("<wp:Param>");
124+
var sb = new StringBuilder();
130125

131126
if (!string.IsNullOrEmpty(NavigatePath))
132127
sb.Append(XmlEncode("/" + NavigatePath.TrimStart('/')));
@@ -139,25 +134,20 @@ public override string PayloadToString()
139134
sb.Append(XmlEncode(key + "=" + Parameters[key].ToString()) + "&amp;");
140135
}
141136

142-
sb.AppendLine("</wp:Param>");
137+
var paramValue = sb.ToString();
138+
139+
if (!string.IsNullOrEmpty(paramValue))
140+
toast.Add(new XElement(wp + "Param", paramValue));
143141
}
144142
}
145143

146-
sb.AppendLine("</wp:Toast>");
147-
sb.AppendLine("</wp:Notification>");
148-
149-
return sb.ToString();
144+
notification.Add(toast);
145+
return notification.ToString();
150146
}
151147
}
152148

153149
public class WindowsPhoneRawNotification : WindowsPhoneNotification
154150
{
155-
public WindowsPhoneRawNotification()
156-
: base()
157-
{
158-
}
159-
160-
161151
public string Raw { get; set; }
162152

163153
public override string PayloadToString()
@@ -168,11 +158,6 @@ public override string PayloadToString()
168158

169159
public class WindowsPhoneTileNotification : WindowsPhoneNotification
170160
{
171-
public WindowsPhoneTileNotification()
172-
: base()
173-
{
174-
}
175-
176161
public string TileId { get; set; } //Secondary tile id, leave blank for application tile
177162

178163
public string BackgroundImage { get; set; }
@@ -194,56 +179,48 @@ public WindowsPhoneTileNotification()
194179

195180
public override string PayloadToString()
196181
{
197-
var sb = new StringBuilder();
198-
199-
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
200-
sb.AppendLine("<wp:Notification xmlns:wp=\"WPNotification\">");
201-
202-
sb.Append("<wp:Tile");
203-
204-
if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
205-
{
206-
if (!string.IsNullOrEmpty(this.TileId))
207-
sb.Append(" Id=\"" + XmlEncode(this.TileId) + "\"");
208-
}
182+
XNamespace wp = "WPNotification";
183+
var notification = new XElement(wp + "Notification", new XAttribute(XNamespace.Xmlns + "wp", "WPNotification"));
209184

210-
sb.AppendLine(">");
185+
var tile = new XElement(wp + "Tile");
186+
187+
if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven && !string.IsNullOrEmpty(this.TileId))
188+
tile.Add(new XAttribute("Id", XmlEncode(this.TileId)));
211189

212190
if (!string.IsNullOrEmpty(BackgroundImage))
213-
sb.AppendLine("<wp:BackgroundImage>" + XmlEncode(this.BackgroundImage) + "</wp:BackgroundImage>");
214-
191+
tile.Add(new XElement(wp + "BackgroundImage", XmlEncode(BackgroundImage)));
192+
215193
if (ClearCount)
216-
sb.AppendLine("<wp:Count Action=\"Clear\"></wp:Count>");
194+
tile.Add(new XElement(wp + "Count", new XAttribute("Action", "Clear")));
217195
else if (Count.HasValue)
218-
sb.AppendLine("<wp:Count>" + Count.ToString() + "</wp:Count>");
196+
tile.Add(new XElement(wp + "Count", XmlEncode(Count.ToString())));
219197

220198
if (ClearTitle)
221-
sb.AppendLine("<wp:Title Action=\"Clear\"></wp:Title>");
199+
tile.Add(new XElement(wp + "Title", new XAttribute("Action", "Clear")));
222200
else if (!string.IsNullOrEmpty(Title))
223-
sb.AppendLine("<wp:Title>" + XmlEncode(Title) + "</wp:Title>");
224-
225-
if (this.OSVersion > WindowsPhoneDeviceOSVersion.Seven)
201+
tile.Add(new XElement(wp + "Title", XmlEncode(Title)));
202+
203+
if (OSVersion > WindowsPhoneDeviceOSVersion.Seven)
226204
{
227-
if (ClearBackBackgroundImage)
228-
sb.AppendLine("<wp:BackBackgroundImage Action=\"Clear\"></wp:BackBackgroundImage>");
229-
else if (!string.IsNullOrEmpty(BackBackgroundImage))
230-
sb.AppendLine("<wp:BackBackgroundImage>" + XmlEncode(BackBackgroundImage) + "</wp:BackBackgroundImage>");
231-
232205
if (ClearBackTitle)
233-
sb.AppendLine("<wp:BackTitle Action=\"Clear\"></wp:BackTitle>");
234-
else if (!string.IsNullOrEmpty(BackTitle))
235-
sb.AppendLine("<wp:BackTitle>" + XmlEncode(BackTitle) + "</wp:BackTitle>");
206+
tile.Add(new XElement(wp + "BackTitle", new XAttribute("Action", "Clear")));
207+
else if (!string.IsNullOrEmpty(Title))
208+
tile.Add(new XElement(wp + "BackTitle", XmlEncode(BackTitle)));
209+
210+
if (ClearBackBackgroundImage)
211+
tile.Add(new XElement(wp + "BackBackgroundImage", new XAttribute("Action", "Clear")));
212+
else if (!string.IsNullOrEmpty(Title))
213+
tile.Add(new XElement(wp + "BackBackgroundImage", XmlEncode(BackBackgroundImage)));
236214

237215
if (ClearBackContent)
238-
sb.AppendLine("<wp:BackContent Action=\"Clear\"></wp:BackContent>");
239-
else if (!string.IsNullOrEmpty(BackContent))
240-
sb.AppendLine("<wp:BackContent>" + XmlEncode(BackContent) + "</wp:BackContent>");
216+
tile.Add(new XElement(wp + "BackContent", new XAttribute("Action", "Clear")));
217+
else if (!string.IsNullOrEmpty(Title))
218+
tile.Add(new XElement(wp + "BackContent", XmlEncode(BackContent)));
219+
241220
}
242221

243-
sb.AppendLine("</wp:Tile>");
244-
sb.AppendLine("</wp:Notification>");
245-
246-
return sb.ToString();
222+
notification.Add(tile);
223+
return notification.ToString();
247224
}
248225
}
249226

@@ -473,8 +450,7 @@ public override string PayloadToString()
473450
tile.Add(new XElement(wp + "Count", new XAttribute("Action", "Clear")));
474451
else if (Count.HasValue)
475452
tile.Add(new XElement(wp + "Count", XmlEncode(Count.Value.ToString())));
476-
477-
453+
478454
if (ClearCycleImage1)
479455
tile.Add(new XElement(wp + "CycleImage1", new XAttribute("Action", "Clear")));
480456
else if (!string.IsNullOrEmpty(Title))
@@ -520,7 +496,6 @@ public override string PayloadToString()
520496
else if (!string.IsNullOrEmpty(Title))
521497
tile.Add(new XElement(wp + "CycleImage9", XmlEncode(CycleImage9)));
522498

523-
524499
notification.Add(tile);
525500

526501
return notification.ToString();

PushSharp.WindowsPhone/WindowsPhonePushService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace PushSharp.WindowsPhone
99
public class WindowsPhonePushService : PushServiceBase<WindowsPhonePushChannelSettings>
1010
{
1111
public WindowsPhonePushService(WindowsPhonePushChannelSettings channelSettings, PushServiceSettings serviceSettings = null)
12-
: base(channelSettings, serviceSettings)
12+
: base(channelSettings ?? new WindowsPhonePushChannelSettings(), serviceSettings)
1313
{
1414
}
1515

0 commit comments

Comments
 (0)