Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to send notifications to different devices using UrbanAirship

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 273
    Comment on it

    Urban Airship provides a number of REST API endpoints, collectively known as the Urban Airship API Version 3.  It provide different different rest API such as Push,Send Push,Validate,Push Object etc.

    Here below is the example of "Push" API which is used to send notifications to different devices.

    For sending notifications using UrbanAirship we require data to post  i.e Alert, Device app Id, ApplicationKey and ApplicationMasterSecret.
    For different type of devices we have to pass different json.

    Here below is the example:

    	string json = string.Empty; // make json variable to store json for different devices
    	string Badge="1";
    	string Alert="notification test  message";
    	string DevciceAppId="your device id";
    	string ApplicationKey="your application key";
    	string ApplicationMasterSecret="your application master secret";
    	if ((DeviceType.ToUpper()).Equals("IPHONE"))
    	   json = String.Format("{{\"aps\": {{\"badge\": {0},\"alert\": \"{1}\"}}, \"device_tokens\": [\"{2}\"]}}", Badge, Alert,  DevciceAppId);
    	else if (DeviceType.ToUpper().Equals ("ANDROID"))
    	   json = string.Format("{{\"android\": {{\"alert\": \"{0}\"}}, \"apids\": [\"{1}\"]}}", Alert, DevciceAppId);
    	else if (item.DeviceType.ToUpper().Equals ("BLACKBERRY"))
    		json = string.Format("{{\"blackberry\": {{\"content-type\": \"text/plain\", \"body\": \"{0}\"}}, \"device_pins\": [\"{1}\"]}}", Alert, DevciceAppId);
    	if (!string.IsNullOrEmpty(json))
    		// Create a request using a URL that can receive a post. 
    		WebRequest request = WebRequest.Create("");
    		//Application key and master secret
    	   request.Credentials = new NetworkCredential(ApplicationKey, ApplicationMasterSecret );
    		// Set the Method property of the request to POST.
    		request.Method = "POST";
    		// Create POST data and convert it to a byte array.
    		string postData = json;
    		byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    		// Set the ContentType property of the WebRequest.
    		request.ContentType = "application/json";
    		// Set the ContentLength property of the WebRequest.
    		request.ContentLength = byteArray.Length;
    		// Get the request stream.
    		Stream dataStream = request.GetRequestStream();
    		// Write the data to the request stream.
    		dataStream.Write(byteArray, 0, byteArray.Length);
    		// Close the Stream object.
    		// Get the response.
    		WebResponse response = request.GetResponse();
    		// Display the status.
    		// Get the stream containing content returned by the server.
    		dataStream = response.GetResponseStream();
    		// Open the stream using a StreamReader for easy access.
    		StreamReader reader = new StreamReader(dataStream);
    		// Read the content.
    		string responseFromServer = reader.ReadToEnd();
    		// Display the content.
    		// Clean up the streams.
    catch (Exception ex)
    	//log exception

    Referred Link:

    Hope this code will help you. Thanks

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: