<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>//InterKnowlogy/ Blogs &#187; Carmine Sampogna</title>
	<atom:link href="http://blogs.interknowlogy.com/author/csampogna/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.interknowlogy.com</link>
	<description>Blogging the Art of Software</description>
	<lastBuildDate>Fri, 17 May 2013 18:10:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Multi-app support on a single Windows Phone 7 push notifications server</title>
		<link>http://blogs.interknowlogy.com/2012/01/30/multi-app-support-on-a-single-windows-phone-7-push-notifications-server/</link>
		<comments>http://blogs.interknowlogy.com/2012/01/30/multi-app-support-on-a-single-windows-phone-7-push-notifications-server/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 23:50:53 +0000</pubDate>
		<dc:creator>Carmine Sampogna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.interknowlogy.com/?p=2747</guid>
		<description><![CDATA[Windows Phone 7 push notifications are tricky. Take Microsoft&#8217;s clear-as-mudd &#8220;overview&#8221; diagram, for example&#8230; All sorts of colors and arrows going on there. The green parts represent code that you write, and the blue parts represent what Microsoft already has in place. The numbers and arrows represent the awkward dance between them. I&#8217;ll try to [...]]]></description>
			<content:encoded><![CDATA[<p>Windows Phone 7 push notifications are tricky. Take Microsoft&#8217;s clear-as-mudd &#8220;overview&#8221; diagram, for example&#8230;</p>
<p><img src="http://i.msdn.microsoft.com/dynimg/IC505514.jpg" alt="Notifications Diagram" /></p>
<p>All sorts of colors and arrows going on there. The green parts represent code that you write, and the blue parts represent what Microsoft already has in place. The numbers and arrows represent the awkward dance between them. I&#8217;ll try to explain&#8230;</p>
<p>First, in your application (<span style="color: #33ce30;">green<span style="color: #000000;"> side of step 1</span></span>) you have to use Microsoft&#8217;s client side portion of their Push Notification Service, which is the <span style="color: #00ccff;">HTTPNotificationChannel</span> class (<span style="color: #3366ff;">blue</span> side of step 1) , to see if the app already has an open notification channel with Microsoft&#8217;s service (that&#8217;s the MPNS box in the bottom right). Only one channel per application is allowed, and you can use that single channel for all of your apps Push Notification needs.</p>
<p>If there is no channel open already, you can go ahead and use the aforementioned <span style="color: #00ccff;">HTTPNotificationChannel</span> class to set one up. This is step 2 in the diagram above, and here is a sample of it in code&#8230;.</p>
<p>*I used <a href="http://benjii.me/2010/12/push-notifications-in-windows-phone-7-1-code-on-the-device/" title="this" target="_blank">this</a> tutorial by Ben Cull for most of the code you&#8217;re going to see, until we get to the &#8220;multi-app&#8221; support on the server.</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">
private void RegisterDevice()
		{
			HttpNotificationChannel httpChannel;
			httpChannel = HttpNotificationChannel.Find(&quot;TestChannel&quot;);

			if (httpChannel != null)
			{
				SubscribeToChannelEvents();
				SubscribeToService();
			}
			else
			{
				httpChannel = new HttpNotificationChannel(&quot;TestChannel&quot;, &quot;TestService&quot;);
				SubscribeToChannelEvents();
				httpChannel.Open();
				if (!httpChannel.IsShellToastBound)
				{
					httpChannel.BindToShellToast();
				}
				if (!httpChannel.IsShellTileBound)
				{
					var c = new System.Collections.ObjectModel.Collection&lt;Uri&gt;();
					c.Add(new Uri(&quot;http://0.tqn.com/&quot;));
					httpChannel.BindToShellTile(c);
				}
			}
		}
</pre>
<p>The call to the &#8220;SubscribeToChannelEvents()&#8221; method above is represented by step 3 in the diagram, and here&#8217;s what it looks like in code&#8230;</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">
private void SubscribeToChannelEvents()
		{
			httpChannel.ChannelUriUpdated += new EventHandler&lt;NotificationChannelUriEventArgs&gt;(httpChannel_ChannelUriUpdated);
			httpChannel.HttpNotificationReceived += new EventHandler&lt;HttpNotificationEventArgs&gt;(httpChannel_HttpNotificationReceived);
			httpChannel.ShellToastNotificationReceived += new EventHandler&lt;NotificationEventArgs&gt;(httpChannel_ShellToastNotificationReceived);
			httpChannel.ErrorOccurred += new EventHandler&lt;NotificationChannelErrorEventArgs&gt;(httpChannel_ErrorOccurred); 
		}
</pre>
<p>Step 4 in the diagram is sending your notification channel&#8217;s URI (it gets this URI through magic during step 2). This is where you need to have a WCF REST Service set up with a database to hold your device id&#8217;s and URI&#8217;s. The code below shows how to subscribe to the service from the client side, but if you want the server side setup and registration you can find all of that in Ben Cull&#8217;s blog post <a href="http://benjii.me/2011/01/push-notifications-in-windows-phone-7-2-code-on-the-server/">here</a></p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">
public string ParseANID(string anid) 
		{ 
			if (!String.IsNullOrEmpty(anid)) 
			{ 
				string[] parts = anid.Split('&amp;'); 
				IEnumerable&lt;string[]&gt; pairs = parts.Select(part =&gt; part.Split('=')); 
				string id = pairs.Where(pair =&gt; pair.Length == 2 &amp;&amp; pair[0] == &quot;A&quot;).Select(pair =&gt; pair[1]).FirstOrDefault(); 
				return id; } return String.Empty; 
		}

		private void SubscribeToService()
		{
			if (!NetworkInterface.GetIsNetworkAvailable())
			{
				return;
			}

			string id = ParseANID(UserExtendedProperties.GetValue(&quot;ANID&quot;) as string);
			var emulatorUrl = string.Format(&quot;http://localhost:55875/notifications/register?deviceid={0}&amp;uri={1}&quot;, id, httpChannel.ChannelUri.ToString());
			var wc = new WebClient();
			wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
			wc.DownloadStringAsync(new Uri(emulatorUrl));
		}
</pre>
<p>The &#8220;ANID&#8221; that you see above is pretty much a unique guid generated for each user&#8217;s Live ID. Also, the emulatorUrl variable above is assuming that you&#8217;re running the WCF service on the same machine that you&#8217;re writing the client side phone code on, and that you&#8217;re using the phone emulator to debug. If you&#8217;re using an actual device to debug than &#8220;localhost&#8221; will produce nothing but disappointment, so adjust your url as needed. At this point, if you followed the WCF REST Service tutorial i linked to above, you&#8217;ll have the infrastructure in place to send push notifications, but no actual way of doing so yet. </p>
<p>For that we&#8217;ll need a service running on the server, with a timer that goes out and determines if there&#8217;s any updates it needs to send to anyone (for example, a weather service that checks for severe weather wvery 30 minutes based on a zip code sent by the client side phone app). But all of this is for a single app. If I wanted to do push notifications again in the future i would have to do this all over again for each new app. Instead, I decided to use a single REST Service, web service, and central device database. The service doesn&#8217;t contain the timer with the application logic. Instead, each app that you want to support is represented by a class that you instantiate in the OnStart() method of the service, and each app class contains its own timer, logic, and its own database with device id&#8217;s and any parameters required for it to work. For the sake of simplicity below are 2 examples of test apps that use the AppName property of the central database rather than their own seperate databases&#8230;.</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">

namespace PushNotificationService.Apps
{
	public class TestApp
	{
		private WP7AppDBEntities _dbContext;
		private readonly string _appName = &quot;TestApp&quot;;

		public TestApp()
		{
			_dbContext = new WP7AppDBEntities();
			var t = new Timer();
			t.Interval = 10000;
			t.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
			t.Start();

		}

		private void OnTimerElapsed(object sender, ElapsedEventArgs e)
		{
			_dbContext.Devices.Where(d =&gt; d.AppName == _appName).ToList().ForEach(d =&gt;
			                                                                      	{
			                                                                      		try
			                                                                      		{
			                                                                      			PushNotifications.SendToast(d.URI, &quot;Test&quot;,
			                                                                      			                            &quot;Hello&quot;);
			                                                                      		}
																						catch{}
			                                                                      	});
		}
	}
}
</pre>
<pre class="brush: csharp; gutter: false; title: ; notranslate">

namespace PushNotificationService.Apps
{
	public class TestApp2
	{
		private WP7AppDBEntities _dbContext;
		private readonly string _appName = &quot;TestApp2&quot;;

		public TestApp2()
		{
			_dbContext = new WP7AppDBEntities();
			var t = new Timer();
			t.Interval = 20000;
			t.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
			t.Start();

		}

		private void OnTimerElapsed(object sender, ElapsedEventArgs e)
		{
			_dbContext.Devices.Where(d =&gt; d.AppName == _appName).ToList().ForEach(d =&gt;
			                                                                      	{
			                                                                      		try
			                                                                      		{
			                                                                      			PushNotifications.SendToast(d.URI,
			                                                                      			                            &quot;TestApp2!&quot;,
			                                                                      			                            &quot;It's working!&quot;);
			                                                                      		}
																						catch{}
			                                                                      	});
		}
	}
}

</pre>
<p>The PushNotifications.SendToast method that you see above represents step 5 of the diagram, and is provided in detail <a href="http://benjii.me/2011/04/push-notifications-in-windows-phone-7-3-push-that-notification/">here</a>.</p>
<p>And here are the classes being instantiated in the OnStart() method of the service&#8230;</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">
protected override void OnStart(string[] args)
		{
			var testApp = new TestApp();
			var testApp2 = new TestApp2();
			
		}

</pre>
]]></content:encoded>
			<wfw:commentRss>http://blogs.interknowlogy.com/2012/01/30/multi-app-support-on-a-single-windows-phone-7-push-notifications-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Push Notifications on Windows Phone 7.</title>
		<link>http://blogs.interknowlogy.com/2011/12/09/push-notifications-on-windows-phone-7/</link>
		<comments>http://blogs.interknowlogy.com/2011/12/09/push-notifications-on-windows-phone-7/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 19:08:19 +0000</pubDate>
		<dc:creator>Carmine Sampogna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.interknowlogy.com/?p=2527</guid>
		<description><![CDATA[While the MSDN has plenty of documentation on what Windows Phone 7 Push Notifications are, what they do, and how they work, they didn&#8217;t have much in terms of how to set up a web service for Push Notifications. I found an awesome tutorial here&#8230;http://benjii.me/2011/01/push-notifications-in-windows-phone-7-2-code-on-the-server/. It&#8217;s a 3 part series that shows everything from building your [...]]]></description>
			<content:encoded><![CDATA[<p>While the MSDN has plenty of documentation on what Windows Phone 7 Push Notifications are, what they do, and how they work, they didn&#8217;t have much in terms of how to set up a web service for Push Notifications. I found an awesome tutorial here&#8230;<a href="http://benjii.me/2011/01/push-notifications-in-windows-phone-7-2-code-on-the-server/">http://benjii.me/2011/01/push-notifications-in-windows-phone-7-2-code-on-the-server/</a>. It&#8217;s a 3 part series that shows everything from building your web service, registering your phone with the web service, and sending push notifications to Microsoft&#8217;s Push Notification system from your web service. The only thing missing from the posts is the few lines of code needed to enable the usage of web images as background tiles. Here&#8217;s that block of code&#8230;.</p>
<pre class="brush: csharp; gutter: false; title: ; notranslate">
if (!httpChannel.IsShellTileBound)

{
     var c = new System.Collections.ObjectModel.Collection&lt;Uri&gt;();
     c.Add(newUri(&quot;http://0.tqn.com/&quot;));
     httpChannel.BindToShellTile(c);

}
</pre>
<p>To enable external images, create a collection of Uri&#8217;s and add each domain that will be hosting an external image that you want to use (domain names are limited to 256 characters). Then, pass the collection to the BindToShellTile method on your http channel. The server side code for updating the tile is in part 3 of the blog post mentioned above.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.interknowlogy.com/2011/12/09/push-notifications-on-windows-phone-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Beginning CSS</title>
		<link>http://blogs.interknowlogy.com/2011/11/03/beginning-css/</link>
		<comments>http://blogs.interknowlogy.com/2011/11/03/beginning-css/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 16:05:46 +0000</pubDate>
		<dc:creator>Carmine Sampogna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blogs.interknowlogy.com/?p=2167</guid>
		<description><![CDATA[The latest project that I&#8217;m working on here at IK requires quite a bit of CSS. The issue with that was that I&#8217;ve never used CSS before in my life. Like anyone else faced with learning a new technology in a relatively short period of time, I found myself asking &#8220;Where do I begin?&#8221;. Luckily [...]]]></description>
			<content:encoded><![CDATA[<p>The latest project that I&#8217;m working on here at IK requires quite a bit of CSS. The issue with that was that I&#8217;ve never used CSS before in my life. Like anyone else faced with learning a new technology in a relatively short period of time, I found myself asking &#8220;Where do I begin?&#8221;. Luckily it didn&#8217;t take long to find my starting point, so while I&#8217;m not yet making ultra fancy websites I <em>can </em>recommend the following website to anyone searching for a good entry point into CSS&#8230;.. <a href="http://www.w3schools.com/css/default.asp">http://www.w3schools.com/css/default.asp</a>.  A couple hours of these tutorials and you&#8217;ll know enough CSS to be dangerous. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.interknowlogy.com/2011/11/03/beginning-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
