Archive

Archive for April, 2013

Android Push Notification ASP.NET C#

April 23, 2013 2 comments

The below code will send push notification to an android phone using ASP.NET C#

string GoogleAppID = “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”; //API Key

string DeviceId= “XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;  //Device Token

string msg = “Message from ASP.NET”

WebRequest tRequest;

tRequest =

WebRequest.Create(“https://android.googleapis.com/gcm/send“);

tRequest.Method =

“post”;

tRequest.ContentType =

“application/x-www-form-urlencoded;charset=UTF-8”;

tRequest.Headers.Add(

string.Format(“Authorization: key={0}”, GoogleAppID));

String collaspeKey = Guid.NewGuid().ToString(“n”);

String postData = string.Format(“registration_id={0}&data.msg={1}&collapse_key={2}”, DeviceID, msg, collaspeKey);

Byte[] byteArray = Encoding.UTF8.GetBytes(postData);

tRequest.ContentLength = byteArray.Length;

Stream dataStream = tRequest.GetRequestStream();

dataStream.Write(byteArray, 0, byteArray.Length);

dataStream.Close();

WebResponse tResponse = tRequest.GetResponse();

dataStream = tResponse.GetResponseStream();

StreamReader tReader = newStreamReader(dataStream);

String sResponseFromServer = tReader.ReadToEnd();

tReader.Close();

dataStream.Close();

tResponse.Close();

🙂

There was an error deserializing the object of type WebService ResponseBody. The maximum array length quota (16384) has been exceeded while reading XML data. This quota may be increased by changing the MaxArrayLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1, position 49035.’. Please see InnerException for more details.

April 20, 2013 1 comment

You can resolve this error by adding these lines at client app.config or web.config

<readerQuotas maxDepth=”32″ maxStringContentLength=”83886088″
maxArrayLength=”163848″ maxBytesPerRead=”40969″
maxNameTableCharCount=”163849″ />

so the full setting as follows

<bindings>

<basicHttpBinding>

<binding name=”PATWebServiceSoap” >

<readerQuotas maxDepth=”32″ maxStringContentLength=”83886088″             maxArrayLength=”163848″ maxBytesPerRead=”40969″             maxNameTableCharCount=”163849″ />

</binding>

</basicHttpBinding>

</bindings>

Happy coding:)

Categories: WPF

IPhone Push Notification from ASP.NET C# Moon APNS

April 18, 2013 3 comments

Consider a scenario, I need to send a push notification to IPhone when I add a new card (card id and card name) at back end(from ASP.NET C# Web application)

At First, reference these Moon APNS dlls.

http://sdrv.ms/17sB454

Try the code below, it fetches all the iphone device ids from table, and send message to each devices.

internal void SendPushNotificationIPhone(int CardId, string CardName, string Path)

{

try

{

DataTable tab = datalogic.GetDeviceTokens(); // Getting all device ids from the table

var p = new List<NotificationPayload>();

for (int i = 0; i < tab.Rows.Count; i++)

{

string message = “Card Added”;

var payload = new NotificationPayload(tab.Rows[i][0].ToString(), message, 1, “default”);

payload.AddCustom(“ID”, CardId);   // Custom fields as id and card name

payload.AddCustom(“CardName”, CardName);

p.Add(payload);

}

var push = new PushNotification(true, Path, “password”);

//path is iphone app’s p12 certificate file, put password for that certificate also,

var rejected = push.SendToApple(p);  // error list

foreach (var item in rejected)

{

Console.WriteLine(item);

}

//success;

}

catch (Exception ep)

{

//  Error

}

}

Categories: ASP.NET

Error 7 Custom tool error: Failed to generate code for the service reference ‘PService’. Please check other error and warning messages for details. C:\Users\ramin\Documents\Visual Studio 2010\Projects\P\P\Service References\Project\Reference.svcmap

April 7, 2013 2 comments

To resolve this,

Right click service -> Configure Service Reference -> Uncheck Reuse types in referenced assemblies -> Click OK

Right click web service -> Update service reference.

thats it.

Happy Coding:)