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
    • 223
    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:

    try
    {
    	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("https://go.urbanairship.com/api/push/");
    
    		//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.
    		//WRITE JSON DATA TO VARIABLE postData
    		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.
    		dataStream.Close();
    		// Get the response.
    		WebResponse response = request.GetResponse();
    		// Display the status.
    		Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    		// 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.
    		Console.WriteLine(responseFromServer);
    		// Clean up the streams.
    		reader.Close();
    		dataStream.Close();
    		response.Close();
    
    	}
    }
    catch (Exception ex)
    {
    	
    	//log exception
    }

    Referred Link: http://docs.urbanairship.com/api/ua.html

    Hope this code will help you. Thanks

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: