<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>El blog de'n Carles</title>
	
	<link>http://blog.company.cat</link>
	<description>Blog personal</description>
	<lastBuildDate>Tue, 08 May 2012 08:04:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ElBlogDenCarles" /><feedburner:info uri="elblogdencarles" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Trello</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/PMLHfTA8X7Y/</link>
		<comments>http://blog.company.cat/2012/05/trello/#comments</comments>
		<pubDate>Tue, 08 May 2012 07:59:58 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[internet]]></category>
		<category><![CDATA[trello]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=396</guid>
		<description><![CDATA[Trello es una taula de kanban online per a petits grups. Serveix per organitzar les tasques en diferents llistes i així tenir una millor visualització del que hem de fer. A més, ens permet gestionar equips de treball assignant les &#8230; <a href="http://blog.company.cat/2012/05/trello/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="https://trello.com/">Trello</a> es una <a href="http://en.wikipedia.org/wiki/Kanban_board">taula de kanban</a> online per a petits grups. Serveix per organitzar les tasques en diferents llistes i així tenir una millor visualització del que hem de fer. A més, ens permet gestionar equips de treball assignant les tasques a les diferents persones que formen el nostre equip. <span id="more-396"></span></p>
<p>L&#8217;aspecte de Trello és el següent:</p>
<p><a href="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Test.png"><img src="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Test-300x131.png" alt="" title="Trello" width="300" height="131" class="alignnone size-medium wp-image-399" /></a></p>
<p>Com podem veure tenim una serie de llistes i unes targetes asociades a aquestes. La gracia és que podem definir les llistes que volguem i canviar-ne el nom per adaptar-les a la nostra manera de fer feina:</p>
<p><a href="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Mesllistes.png"><img src="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Mesllistes-300x132.png" alt="Més llisrtes" title="Trello" width="300" height="132" class="alignnone size-medium wp-image-401" /></a></p>
<p>La idea és anar moguent les targetes a la llista que li pertoca depenent del seu estat. Així tot l&#8217;equip pot tenir una idea de l&#8217;estat de totes les tasques:</p>
<p><a href="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Moure.png"><img src="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Moure-300x132.png" alt="" title="Trello moure" width="300" height="132" class="alignnone size-medium wp-image-402" /></a></p>
<p>A més, a cada targeta hi podem afegir molta informació i permetre a lagent afegir-hi comentaris o votar per les tasques que volen que es completin antes:</p>
<p><a href="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Detalls.png"><img src="http://blog.company.cat/wp-content/uploads/2012/05/Trello-Detalls-300x243.png" alt="" title="Trello detalls" width="300" height="243" class="alignnone size-medium wp-image-403" /></a></p>
<p>Fins aquí aquesta petita introducció a Trello. Recordeu que la idea és que l&#8217;eina s&#8217;adapti a la vostra manera de treballar i Trello és prou flexible per que així sigui.</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/PMLHfTA8X7Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2012/05/trello/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2012/05/trello/</feedburner:origLink></item>
		<item>
		<title>Enviar missatges a mòbils Android emprant HttpClient</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/jtZ8lNrfzSU/</link>
		<comments>http://blog.company.cat/2012/04/enviar-missatges-a-mobils-android-emprant-httpclient/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 15:08:02 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[programació]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=380</guid>
		<description><![CDATA[Emprant les noves llibreries de ASP.Net Web API, el codi per enviar missatges a mòvils android que varem veure aquí pot quedar molt més senzill. El codi per autentificar queda així: 123456789101112131415161718192021public string GetToken&#40;string email, string password, string source&#41; &#123; &#8230; <a href="http://blog.company.cat/2012/04/enviar-missatges-a-mobils-android-emprant-httpclient/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Emprant les noves llibreries de ASP.Net Web API, el codi per enviar missatges a mòvils android que varem veure <a href="http://blog.company.cat/2012/02/enviar-missatges-push-a-un-mobil-android-c2dm/">aquí</a> pot quedar molt més senzill.<br />
<span id="more-380"></span></p>
<p>El codi per autentificar queda així:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetToken<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> email, <span style="color: #6666cc; font-weight: bold;">string</span> password, <span style="color: #6666cc; font-weight: bold;">string</span> source<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; HttpContent content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FormUrlEncodedContent<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Email&quot;</span>, email<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Passwd&quot;</span>, password<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;accountType&quot;</span>, <span style="color: #666666;">&quot;GOOGLE&quot;</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;source&quot;</span>, source<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;service&quot;</span>, <span style="color: #666666;">&quot;ac2dm&quot;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var req <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">PostAsync</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span>, content<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">ContinueWith</span><span style="color: #008000;">&#40;</span>request <span style="color: #008000;">=&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var stringResp <span style="color: #008000;">=</span> request<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAsStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContinueWith</span><span style="color: #008000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Auth=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> stringResp<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> req<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Com veim, el codi queda molt més compacte i fàcil d&#8217;entendre. A més, en podem fer una versió asincrona emprant el CTP de les llibreries de la TPL (veure <a href="http://msdn.microsoft.com/en-us/vstudio/gg316360">aquí</a>):</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> async Task<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> GetTokenAsync<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> email, <span style="color: #6666cc; font-weight: bold;">string</span> password, <span style="color: #6666cc; font-weight: bold;">string</span> source<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var client <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; HttpContent content <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FormUrlEncodedContent<span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Email&quot;</span>, email<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;Passwd&quot;</span>, password<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;accountType&quot;</span>, <span style="color: #666666;">&quot;GOOGLE&quot;</span><span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;source&quot;</span>, source<span style="color: #008000;">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;service&quot;</span>, <span style="color: #666666;">&quot;ac2dm&quot;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var req <span style="color: #008000;">=</span> await client<span style="color: #008000;">.</span><span style="color: #0000FF;">PostAsync</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span>, content<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var res <span style="color: #008000;">=</span> await req<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAsStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> res<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>s <span style="color: #008000;">=&gt;</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Auth=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Per enviar el missatge en si, també ens queda el codi molt més clar:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> SendMessageDataContract SendMessage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> authToken, <span style="color: #6666cc; font-weight: bold;">string</span> registrationId, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> content<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var client<span style="color: #008000;">=</span><span style="color: #008000;">new</span> HttpClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var nameValueCollection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;registration_id&quot;</span>, registrationId<span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;collapse_key&quot;</span>, <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var kvp <span style="color: #0600FF; font-weight: bold;">in</span> content<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; nameValueCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;data.&quot;</span> <span style="color: #008000;">+</span> kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span>, kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; var postContent <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FormUrlEncodedContent<span style="color: #008000;">&#40;</span>nameValueCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var request <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpRequestMessage<span style="color: #008000;">&#40;</span>HttpMethod<span style="color: #008000;">.</span><span style="color: #0000FF;">Post</span>, <span style="color: #666666;">&quot;https://android.clients.google.com/c2dm/send&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content <span style="color: #008000;">=</span> postContent<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">+=</span> ValidationCallback<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; request<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>HttpRequestHeader<span style="color: #008000;">.</span><span style="color: #0000FF;">Authorization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;GoogleLogin auth={0}&quot;</span>, authToken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var res <span style="color: #008000;">=</span> client<span style="color: #008000;">.</span><span style="color: #0000FF;">SendAsync</span><span style="color: #008000;">&#40;</span>request<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">ContinueWith</span><span style="color: #008000;">&#40;</span>t <span style="color: #008000;">=&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var responseCode <span style="color: #008000;">=</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> errorString<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2DMResponseCode c2DMResponse<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAsStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ContinueWith</span><span style="color: #008000;">&#40;</span>s <span style="color: #008000;">=&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var error <span style="color: #008000;">=</span> s<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span>r <span style="color: #008000;">=&gt;</span> r<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> error <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> error<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">6</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2DMResponse<span style="color: #008000;">=</span>C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Ok</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">NotImplemented</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Server unavailable.&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2DMResponse <span style="color: #008000;">=</span> C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerUnavailable</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Unauthorized</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Invalid AUTH_TOKEN&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2DMResponse <span style="color: #008000;">=</span> C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">InvalidAuthToken</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Unspecified error&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2DMResponse<span style="color: #008000;">=</span>C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> SendMessageDataContract<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResponseCode <span style="color: #008000;">=</span> responseCode,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2DMResponseCode <span style="color: #008000;">=</span> c2DMResponse,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Error<span style="color: #008000;">=</span>errorString,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateClient <span style="color: #008000;">=</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Update-Client-Auth&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Update-Client-Auth&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> res<span style="color: #008000;">.</span><span style="color: #0000FF;">Result</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>o la versió asincrona:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> async Task<span style="color: #008000;">&lt;</span>SendMessageDataContract<span style="color: #008000;">&gt;</span> SendMessageAsync<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> authToken, <span style="color: #6666cc; font-weight: bold;">string</span> registrationId, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> content<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var client<span style="color: #008000;">=</span><span style="color: #008000;">new</span> HttpClient<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var nameValueCollection <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;registration_id&quot;</span>, registrationId<span style="color: #008000;">&#125;</span>, <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;collapse_key&quot;</span>, <span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var kvp <span style="color: #0600FF; font-weight: bold;">in</span> content<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; nameValueCollection<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;data.&quot;</span> <span style="color: #008000;">+</span> kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span>, kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; var postContent <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FormUrlEncodedContent<span style="color: #008000;">&#40;</span>nameValueCollection<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var request <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpRequestMessage<span style="color: #008000;">&#40;</span>HttpMethod<span style="color: #008000;">.</span><span style="color: #0000FF;">Post</span>, <span style="color: #666666;">&quot;https://android.clients.google.com/c2dm/send&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content <span style="color: #008000;">=</span> postContent<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">+=</span> ValidationCallback<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; request<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>HttpRequestHeader<span style="color: #008000;">.</span><span style="color: #0000FF;">Authorization</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;GoogleLogin auth={0}&quot;</span>, authToken<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var res <span style="color: #008000;">=</span> await client<span style="color: #008000;">.</span><span style="color: #0000FF;">SendAsync</span><span style="color: #008000;">&#40;</span>request<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var responseCode <span style="color: #008000;">=</span> res<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">string</span> errorString<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; C2DMResponseCode c2DMResponse<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">OK</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; var errorLine<span style="color: #008000;">=</span><span style="color: #008000;">&#40;</span>await res<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAsStringAsync</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'<span style="color: #008080; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FirstOrDefault</span><span style="color: #008000;">&#40;</span>r <span style="color: #008000;">=&gt;</span> r<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> errorLine <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">?</span> errorLine<span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">6</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; c2DMResponse<span style="color: #008000;">=</span>C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Ok</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">NotImplemented</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Server unavailable.&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; c2DMResponse <span style="color: #008000;">=</span> C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerUnavailable</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Unauthorized</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Invalid AUTH_TOKEN&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; c2DMResponse<span style="color: #008000;">=</span>C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">InvalidAuthToken</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; errorString <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Unspecified error&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; c2DMResponse <span style="color: #008000;">=</span> C2DMResponseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Error</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> SendMessageDataContract<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ResponseCode <span style="color: #008000;">=</span> responseCode,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; C2DMResponseCode<span style="color: #008000;">=</span>c2DMResponse,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Error <span style="color: #008000;">=</span> errorString,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UpdateClient <span style="color: #008000;">=</span> res<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Update-Client-Auth&quot;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> res<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetValues</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Update-Client-Auth&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">null</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span><br />
&nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>He publicat el codi com una llibreria. La llibreria es diu C2DMNet i la podeu trobar <a href="https://bitbucket.org/carlescs/c2dmnet/overview">aquí</a> o també publicada com a paquets <a href="https://nuget.org/packages?q=c2dmnet">Nuget</a>.</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/jtZ8lNrfzSU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2012/04/enviar-missatges-a-mobils-android-emprant-httpclient/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2012/04/enviar-missatges-a-mobils-android-emprant-httpclient/</feedburner:origLink></item>
		<item>
		<title>Enviar missatges push a un mòbil Android mitjançant C2DM</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/wop7Ula_PyM/</link>
		<comments>http://blog.company.cat/2012/02/enviar-missatges-push-a-un-mobil-android-c2dm/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 22:03:30 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[programació]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=348</guid>
		<description><![CDATA[Darrerament he estat creant alguns programets Android a la feina i m&#8217;he trobat en la situació de que necessitava enviar avisos del servidor cap al dispositiu. Investigant un poc vaig veure que la manera estandard de fer-ho és emprant C2DM. &#8230; <a href="http://blog.company.cat/2012/02/enviar-missatges-push-a-un-mobil-android-c2dm/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Darrerament he estat creant alguns programets Android a la feina i m&#8217;he trobat en la situació de que necessitava enviar avisos del servidor cap al dispositiu. Investigant un poc vaig veure que la manera estandard de fer-ho és emprant <a href="http://code.google.com/intl/ca/android/c2dm/" title="C2DM">C2DM</a>. La idea és bastant senzilla.<br />
<span id="more-348"></span><br />
Els passos a seguir són:</p>
<ol>
<li>Configurem el programa del dispositiu per rebre missatges de C2DM.</li>
<li>Definim un Listener que respondrà als missatges rebuts del servidor.</li>
<li>Registrem el dispositiu al servidor de Google. Això ens retornarà un identificador únic pel dispositiu.</li>
<li>Enviem l&#8217;identificador al nostre servidor ja que serà els que emprarà per enviar-nos els missatges.</li>
<li>Quan volguem enviar un missatge del servidor al dispositiu, ens hem d&#8217;autentificar contra Google per obtenir un token d&#8217;autentificació. Aquest token es pot cachejar i emprar en crides successives.</li>
<li>Emprant el token d&#8217;autentificació i l&#8217;identificador del dispositiu, podem enviar el missatge al dispositiu mitjançant una crida HTTP POST al servidor C2DM de Google.</li>
</ol>
<p>Hem de tenir en compte que els missatges han de ser parelles de clau &#8211; valor i que el tamany màxim són 1024 bytes pel total del missatge. Per això, és convenient emprar-ho per avisar al dispositiu que ha d&#8217;anar a cercar les dades al servidor, no per enviar les dades en si.</p>
<h3>Configuració AndroidManifest.xml</h3>
<p>Al manifest hi hem de configurar el listener per tractar els missatges rebuts del servidor C2DM. A més hem de donar permís a l&#8217;aplicació per emprar C2DM. Els permisos els definim així:</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dibaelsa.autoritzacions.permission.C2D_MESSAGE&quot;</span> <span style="color: #000066;">android:protectionLevel</span>=<span style="color: #ff0000;">&quot;signature&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/permission<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dibaelsa.autoritzacions.permission.C2D_MESSAGE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses-permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.permission.RECEIVE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></div></td></tr></tbody></table></div>
<p>I definim el listener amb els intents adequats. El listener el definirem al següent apartat.</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.receivers.C2DMReceiver&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dibaelsa.autoritzacions&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dibaelsa.autoritzacions&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent-filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div></td></tr></tbody></table></div>
<h3>Listener</h3>
<p>El listener accepta els missatges rebuts pel servidor i actua en consequencia:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.dibaelsa.autoritzacions.receivers</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.json.JSONException</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.BroadcastReceiver</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.SharedPreferences.Editor</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.AsyncTask</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dibaelsa.autoritzacions.rest.RestManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dibaelsa.autoritzacions.service.AutoritzacionsUpdateService</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2DMReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> TAG <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;c2dm&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;c2dmPref&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> REGISTRATION_KEY <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;registrationKey&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Context</span> context, <span style="color: #000000; font-weight: bold;">final</span> Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AsyncTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Void</span>, <span style="color: #003399;">Void</span>, Void<span style="color: #339933;">&gt;</span> tasca<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> AsyncTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Void</span>, <span style="color: #003399;">Void</span>, Void<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Void</span> doInBackground<span style="color: #009900;">&#40;</span><span style="color: #003399;">Void</span>... <span style="color: #006633;">params</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleRegistration<span style="color: #009900;">&#40;</span>context, intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tasca.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Void</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AsyncTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Void</span>, <span style="color: #003399;">Void</span>, Void<span style="color: #339933;">&gt;</span> tasca<span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> AsyncTask<span style="color: #339933;">&lt;</span><span style="color: #003399;">Void</span>, <span style="color: #003399;">Void</span>, Void<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">Void</span> doInBackground<span style="color: #009900;">&#40;</span><span style="color: #003399;">Void</span>... <span style="color: #006633;">params</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; handleMessage<span style="color: #009900;">&#40;</span>context,intent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tasca.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Void</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> handleMessage<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> payload<span style="color: #339933;">=</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;payload&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">i</span><span style="color: #009900;">&#40;</span>TAG,payload<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; context.<span style="color: #006633;">startService</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, AutoritzacionsUpdateService.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> handleRegistration<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> registration <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;registration_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;registration failed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">String</span> error <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SERVICE_NOT_AVAILABLE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;SERVICE_NOT_AVAILABLE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ACCOUNT_MISSING&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;ACCOUNT_MISSING&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;AUTHENTICATION_FAILED&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;AUTHENTICATION_FAILED&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TOO_MANY_REGISTRATIONS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;TOO_MANY_REGISTRATIONS&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INVALID_SENDER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;INVALID_SENDER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>error.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;PHONE_REGISTRATION_ERROR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;PHONE_REGISTRATION_ERROR&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unregistered&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;unregistered&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>registration <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">d</span><span style="color: #009900;">&#40;</span>TAG, registration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Editor editor <span style="color: #339933;">=</span> context.<span style="color: #006633;">getSharedPreferences</span><span style="color: #009900;">&#40;</span>KEY,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003399;">Context</span>.<span style="color: #006633;">MODE_PRIVATE</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">edit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.<span style="color: #006633;">putString</span><span style="color: #009900;">&#40;</span>REGISTRATION_KEY, registration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RestManager.<span style="color: #006633;">Registra</span><span style="color: #009900;">&#40;</span>context, registration<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Error gravant.&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JSONException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>TAG, <span style="color: #0000ff;">&quot;Error gravant.&quot;</span>, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span style="color: #006633;">e</span><span style="color: #009900;">&#40;</span>TAG,<span style="color: #0000ff;">&quot;Error gravant.&quot;</span>,e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Al missatge rebut hem de distingir entre si es un missatge de registre (quan ens registrem al servidor C2DM i ens retorna l&#8217;ID del dispositiu) o si es un missatge enviat per nosaltres. A més, emprem un thread nou per tractar els missatges perque no ens bloquegi el programa (sobretot si hem de contactar amb el nostre servidor per enviar el RegistrationId). En aquest exemple empr un servei per refrescar les dades del servidor quan reb el missatge que m&#8217;avisa que han canviat i un servei REST per gravar l&#8217;Id del dispositiu al servidor.</p>
<h3>Registre amb el servidor C2DM</h3>
<p>Per registrar el telèfon amb el servidor C2DM emprem un Intent que ens ofereix aquesta funcionalitat:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Intent registrationIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;app&quot;</span>, PendingIntent.<span style="color: #006633;">getBroadcast</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sender&quot;</span>, <span style="color: #339933;">&lt;&lt;</span>adreça<span style="color: #339933;">&gt;&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
startService<span style="color: #009900;">&#40;</span>registrationIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>On <tt>&lt;&lt;adreça&gt;&gt;</tt> és l&#8217;adreça que hem donat d&#8217;alta amb Google per el servei de C2DM. Això ens fara una crida als servidors de Google i aquest ens respondrà amb un identificador únic per el nostre dispositiu. La resposta la tractam al listener que hem creat a l&#8217;apartat anterior.</p>
<h2>Part del servidor</h2>
<p>La part del servidor la escriuré amb C#, però és prou senzilla com per traslladar-ho a qualsevol llenguatge o plataforma. </p>
<h3>Autentificació</h3>
<p>El primer es sol·licitar un token d&#8217;autentificació al servidor de &#8220;Client Auth&#8221; de Google. Això ho fem amb el següent métode:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> GetToken<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> email, <span style="color: #6666cc; font-weight: bold;">string</span> password, <span style="color: #6666cc; font-weight: bold;">string</span> source<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Email=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>email<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;Passwd=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>password<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;accountType=GOOGLE&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;source=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>source<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;service=ac2dm&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var encoding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UTF8Encoding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">=</span> encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>builder<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var url <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var con <span style="color: #008000;">=</span> WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>url<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; con<span style="color: #008000;">.</span><span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; con<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; con<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-www-form-urlencoded&quot;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; Stream output <span style="color: #008000;">=</span> con<span style="color: #008000;">.</span><span style="color: #0000FF;">GetRequestStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; output<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>data, <span style="color: #FF0000;">0</span>, data<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; output<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; WebResponse response <span style="color: #008000;">=</span> con<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var reader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>response<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> reader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLines</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Auth=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Simplement feim una crida <a href="http://en.wikipedia.org/wiki/POST_(HTTP)" title="POST">POST</a> al servei d&#8217;autentificació de Google. Després n&#8217;analitzam la resposta cercant la línia que comença per &#8220;Auth=&#8221; que és la que conté el token d&#8217;autentificació.</p>
<h3>Enviament del missatge</h3>
<p>Un pic tinguem el token d&#8217;autentificació ja podem enviar el missatge. Per això, també hem de fer una crida POST als servidors de Google. Ho feim així:</p>
<div class="codecolorer-container csharp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">public</span> HttpStatusCode SendMessage<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> authToken, <span style="color: #6666cc; font-weight: bold;">string</span> registrationId, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> content, <span style="color: #0600FF; font-weight: bold;">out</span> <span style="color: #6666cc; font-weight: bold;">string</span> error<span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; var postDataBuilder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; postDataBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>ParamRegistrationId<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;=&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>registrationId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; postDataBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>ParamCollapseKey<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;=&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;0&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var kvp <span style="color: #0600FF; font-weight: bold;">in</span> content<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; postDataBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;data.&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Key</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;=&quot;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span>HttpUtility<span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncodeUnicode</span><span style="color: #008000;">&#40;</span>kvp<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; var encoding <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UTF8Encoding<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> postData <span style="color: #008000;">=</span> encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span>postDataBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var url <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Uri<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;https://android.clients.google.com/c2dm/send&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">+=</span> ValidationCallback<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var conn <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>HttpWebRequest<span style="color: #008000;">&#41;</span>WebRequest<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>url<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; conn<span style="color: #008000;">.</span><span style="color: #0000FF;">Proxy</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; conn<span style="color: #008000;">.</span><span style="color: #0000FF;">Method</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;POST&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; conn<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/x-www-form-urlencoded;charset=UTF-8&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; conn<span style="color: #008000;">.</span><span style="color: #0000FF;">ContentLength</span> <span style="color: #008000;">=</span> postData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; conn<span style="color: #008000;">.</span><span style="color: #0000FF;">Headers</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Authorization&quot;</span>, <span style="color: #666666;">&quot;GoogleLogin auth=&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008000;">+</span> authToken<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var sort <span style="color: #008000;">=</span> conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetRequestStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; sort<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>postData, <span style="color: #FF0000;">0</span>, postData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; sort<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; var httpWebResponse <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>HttpWebResponse<span style="color: #008000;">&#41;</span>conn<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponse</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; var responseCode <span style="color: #008000;">=</span> httpWebResponse<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">200</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var streamReader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #008000;">&#40;</span>httpWebResponse<span style="color: #008000;">.</span><span style="color: #0000FF;">GetResponseStream</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; error <span style="color: #008000;">=</span> streamReader<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLines</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span>t <span style="color: #008000;">=&gt;</span> t<span style="color: #008000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Substring</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">6</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">501</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; error <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Server unavailable.&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #0600FF; font-weight: bold;">if</span><span style="color: #008000;">&#40;</span>responseCode<span style="color: #008000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">401</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; error <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Invalid AUTH_TOKEN&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">else</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; error <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Unspecified error&quot;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">return</span> responseCode<span style="color: #008000;">;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/wop7Ula_PyM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2012/02/enviar-missatges-push-a-un-mobil-android-c2dm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2012/02/enviar-missatges-push-a-un-mobil-android-c2dm/</feedburner:origLink></item>
		<item>
		<title>VirtualBox</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/nkwo3hZhNhM/</link>
		<comments>http://blog.company.cat/2011/09/virtualbox/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 13:39:14 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[internet]]></category>
		<category><![CDATA[eines]]></category>
		<category><![CDATA[virtualització]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=338</guid>
		<description><![CDATA[Si teniu la necessitat d&#8217;instal·lar una màquina virtual per fer proves, un dels millors programes per això és VirtualBox. Aquest programa ens permet crear màquines virtual que s&#8217;executaran sobre el nostre sistema operatiu. A més, si el nostre processador té &#8230; <a href="http://blog.company.cat/2011/09/virtualbox/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Si teniu la necessitat d&#8217;instal·lar una màquina virtual per fer proves, un dels millors programes per això és <a href="http://www.virtualbox.org/" title="VirtualBox">VirtualBox</a>. Aquest programa ens permet crear màquines virtual que s&#8217;executaran sobre el nostre sistema operatiu. A més, si el nostre processador té les extensions de virtualització (Intel-VT o AMD-V), la penalització de velocitat serà mínima. </p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/nkwo3hZhNhM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/09/virtualbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/09/virtualbox/</feedburner:origLink></item>
		<item>
		<title>Vietnam</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/wQFyZbqmKE8/</link>
		<comments>http://blog.company.cat/2011/09/vietnam-2/#comments</comments>
		<pubDate>Sat, 10 Sep 2011 08:41:04 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[fotos]]></category>
		<category><![CDATA[viatges]]></category>
		<category><![CDATA[vietnam]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=336</guid>
		<description />
			<content:encoded><![CDATA[						<div class="flickr-gallery image none"><a href="http://www.flickr.com/photos/carlescs/6106179004"><img class="flickr small" title="Vietnam" alt="Vietnam" src="http://farm7.static.flickr.com/6077/6106179004_3c2f085f9f_m.jpg" /></a></div>
					
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/wQFyZbqmKE8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/09/vietnam-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/09/vietnam-2/</feedburner:origLink></item>
		<item>
		<title>Google+</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/WY-BgoIXhTM/</link>
		<comments>http://blog.company.cat/2011/07/google/#comments</comments>
		<pubDate>Sun, 03 Jul 2011 17:43:54 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[internet]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[social media]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=331</guid>
		<description><![CDATA[Avui he estat provant Google+ i he de dir que m&#8217;he enduit una bona impressió. A primera vista pareix que té el millor de Facebook i el millor de Twitter, amb algunes característiques més. El que més m&#8217;ha cridat l&#8217;atenció &#8230; <a href="http://blog.company.cat/2011/07/google/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Avui he estat provant Google+ i he de dir que m&#8217;he enduit una bona impressió. A primera vista pareix que té el millor de Facebook i el millor de Twitter, amb algunes característiques més. El que més m&#8217;ha cridat l&#8217;atenció és:</p>
<ul>
<li>El sistema de permisos (els cercles). Permeten tenir molta granularitat a l&#8217;hora d&#8217;assignar permisos.</li>
<li>Les quedades. És un sistema de videoconferència múltiple. Encara no l&#8217;he provat, però promet&#8230;</li>
</ul>
<p>Així i tot, encara té algunes mancances com la falta de grups,&#8230; Així i tot, segur que en sentirem parlar molt els mesos vinents i que es convertirà en un dur competidor de Facebook.</p>
<p><strong>Update:</strong><br />
Provant, crec que les converses de twitter són més senzilles de seguir&#8230;</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/WY-BgoIXhTM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/07/google/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/07/google/</feedburner:origLink></item>
		<item>
		<title>Dos videos d’Angry Birds</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/hZuCn8PuPcE/</link>
		<comments>http://blog.company.cat/2011/06/dos-videos-dangry-birds/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 11:33:32 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[dois]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[video]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=324</guid>
		<description><![CDATA[http://www.youtube.com/watch?v=jzIBZQkj6SY http://www.youtube.com/watch?v=7UCm6uyzNE8 Pels qui no conegueu Angry Birds: aquí.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.youtube.com/watch?v=jzIBZQkj6SY">http://www.youtube.com/watch?v=jzIBZQkj6SY</a></p>
<p><a href="http://www.youtube.com/watch?v=7UCm6uyzNE8">http://www.youtube.com/watch?v=7UCm6uyzNE8</a></p>
<p>Pels qui no conegueu Angry Birds: <a href="http://www.rovio.com/index.php?page=angry-birds">aquí</a>.</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/hZuCn8PuPcE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/06/dos-videos-dangry-birds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/06/dos-videos-dangry-birds/</feedburner:origLink></item>
		<item>
		<title>Selectors Css3</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/0VXTZw6ERsc/</link>
		<comments>http://blog.company.cat/2011/06/selectors-css3/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 15:04:46 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[programació]]></category>
		<category><![CDATA[css]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=321</guid>
		<description><![CDATA[Un apunt rapid. M&#8217;encanten els selectors CSS3 que permeten fer coses com: 1234div.noticia:not&#40;:last-child&#41; &#123; &#160; &#160; border-bottom: none; &#125; per llevar la vora de tots els divs de classe notícia menys el darrer.]]></description>
			<content:encoded><![CDATA[<p>Un apunt rapid. M&#8217;encanten els selectors CSS3 que permeten fer coses com:</p>
<div class="codecolorer-container css default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="css codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">div<span style="color: #6666ff;">.noticia</span><span style="color: #00AA00;">:</span>not<span style="color: #00AA00;">&#40;</span><span style="color: #3333ff;">:last-</span>child<span style="color: #00AA00;">&#41;</span><br />
<span style="color: #00AA00;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border-bottom</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span><br />
<span style="color: #00AA00;">&#125;</span></div></td></tr></tbody></table></div>
<p>per llevar la vora de tots els divs de classe notícia menys el darrer.</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/0VXTZw6ERsc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/06/selectors-css3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/06/selectors-css3/</feedburner:origLink></item>
		<item>
		<title>EF 4 i els valors calculats</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/JzMhlVEdsLc/</link>
		<comments>http://blog.company.cat/2011/06/ef-4-i-els-valors-calculats/#comments</comments>
		<pubDate>Wed, 01 Jun 2011 14:05:23 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[feina]]></category>
		<category><![CDATA[programació]]></category>
		<category><![CDATA[EF]]></category>
		<category><![CDATA[trucs]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=317</guid>
		<description><![CDATA[Avui m&#8217;he adonat d&#8217;una cosa que Entity Framework no acaba de fer com toca. Si tenim una columna de tipus FILESTREAM a una BBDD SQL Server 2008, necessitarem també tenir una columna uniqueidentifier per la taula on està. Per inicialitzar &#8230; <a href="http://blog.company.cat/2011/06/ef-4-i-els-valors-calculats/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Avui m&#8217;he adonat d&#8217;una cosa que Entity Framework no acaba de fer com toca. Si tenim una columna de tipus FILESTREAM a una BBDD SQL Server 2008, necessitarem també tenir una columna uniqueidentifier per la taula on està. Per inicialitzar aquesta columna podem donar-li per valor per defecte (newsequentialid()). </p>
<p>Si emprem Entity Framework per accedir a aquesta taula (emprant Database First -el generador de models a partir de BBDD), no ens inicialitzarà com toca el mapejat i ens inserirà sempre el valor 0 a l&#8217;identificador. Per arreglar-ho hem de canviar la propietat StoreGeneratedPattern de la columna uniqueidentifier i posar-ho a Computed.</p>
<p><a href="http://blog.company.cat/wp-content/uploads/2011/06/Captura.png"><img src="http://blog.company.cat/wp-content/uploads/2011/06/Captura-300x171.png" alt="Computed" title="Computed" width="300" height="171" class="alignnone size-medium wp-image-318" /></a></p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/JzMhlVEdsLc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/06/ef-4-i-els-valors-calculats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/06/ef-4-i-els-valors-calculats/</feedburner:origLink></item>
		<item>
		<title>Monitorització de serveis</title>
		<link>http://feedproxy.google.com/~r/ElBlogDenCarles/~3/mjKiMBRP_N4/</link>
		<comments>http://blog.company.cat/2011/05/monitoritzacio-de-serveis/#comments</comments>
		<pubDate>Mon, 23 May 2011 09:30:24 +0000</pubDate>
		<dc:creator>carles</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[programació]]></category>
		<category><![CDATA[informàtica]]></category>
		<category><![CDATA[projectes]]></category>

		<guid isPermaLink="false">http://blog.company.cat/?p=314</guid>
		<description><![CDATA[Estic pensant de crear un petit monitoritzador de serveis. La idea que tenc és crear un petit servei de Windows que ens permeti configurar els serveis a monitoritzar a la màquina on està instal·lat. Després, hi hauria un client que &#8230; <a href="http://blog.company.cat/2011/05/monitoritzacio-de-serveis/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Estic pensant de crear un petit monitoritzador de serveis. La idea que tenc és crear un petit servei de Windows que ens permeti configurar els serveis a monitoritzar a la màquina on està instal·lat. Després, hi hauria un client que enviaria peticions d&#8217;actualització a tots els serveis de Windows que tengués registrats i aquests li contestarien amb l&#8217;estat dels seus serveis. Ara mateix no sé si hi ha alguna manera millor de fer-ho o si hi ha algun producte que implementi aquesta funcionalitat de manera econòmica (no vull un producte que valgui més de 100€ per fer quelcom tan senzill).</p>
<img src="http://feeds.feedburner.com/~r/ElBlogDenCarles/~4/mjKiMBRP_N4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://blog.company.cat/2011/05/monitoritzacio-de-serveis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://blog.company.cat/2011/05/monitoritzacio-de-serveis/</feedburner:origLink></item>
	</channel>
</rss>

