Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

BlackBerry Push Development

Reply
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10
My Carrier: Verizon Wireless 4G LTE network

Working Sample Code of BB 10 Push Notification Servie in Asp.net/c#

Hi Friends,

 

Could you please send/post  working Sample Code of  BB 10 Push Notification Servie in Asp.net/c# ?

We have a  webservice which needs to send push notification for Blacberry 10 users.

 

 

Your help appreciated !!

 

 

Rgds,

Ajait Nair

Highlighted
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10
My Carrier: Verizon Wireless 4G LTE network

Re: Working Sample Code of BB 10 Push Notification Servie in Asp.net/c#

Our Current code  for BES5 push is shown below. Looks like this code is not working for BES10. Can anybody help on this.

 

using System;
using System.Text;
using System.Net;
using System.IO;
 
namespace Helpers
{
   
    publicclassHttpPushWorker
    {
        publicstaticstring[] PUSH_TYPES =newstring[]{"Browser-Channel","Browser-Cache","Browser-Message","Browser-Channel-Message"};
        publicstaticint CHANNEL_INDEX =0;
       
        publicvoid httpPush(stringBESAddress,stringBESWebserverListenPort,int typeIndex,string pushTitle,string url,string readIconURL,string unreadIconURL,string pushPin)
        {
            string pushPort ="7874";
            string pushType = PUSH_TYPES[typeIndex];
 
            //Make a connection to the source page, read it's data and header information
            WebHeaderCollection headers =null;
            StreamReader reader =null;
           
            bool sourceOk = getInputPage(url,ref headers,ref reader);
           
            //Forward that information to MDS to do the push
            if(sourceOk)
            {
                //Create the push URL for MDS this is of the format
                //http://<BESName>:<BESPort>/push?DESTINATTION=<PIN/EMAIL>&PORT=<PushPort>&REQUESTURI=/
                string httpURL ="http://"+BESAddress+":"+BESWebserverListenPort+"/push?DESTINATION="+ pushPin +"&PORT="+ pushPort +"&REQUESTURI=/";
           
                //make the connection
                //we'll put this whole thing in a try/catch block for some basic
                //error handling.
                try
                {
                    HttpWebRequestHttpWReq=(HttpWebRequest)WebRequest.Create(httpURL);
                    HttpWReq.Method=("POST");
 
                    //add the headers nessecary for the push
                    HttpWReq.Headers.Add("Content-Location", url);
                    HttpWReq.Headers.Add("X-RIM-Push-Title", pushTitle);
                    HttpWReq.Headers.Add("X-RIM-Push-Type", pushType);
                    if(pushType.Equals("Browser-Channel")|| pushType.Equals("Browser-Channel-Delete"))
                    {
                        HttpWReq.Headers.Add("X-RIM-Push-Channel-ID", url);
                        if(pushType.Equals("Browser-Channel"))
                        {
                            HttpWReq.Headers.Add("X-RIM-Push-UnRead-Icon-URL", unreadIconURL);
                            HttpWReq.Headers.Add("X-RIM-Push-Read-Icon-URL", readIconURL);
                        }
                    }
                    //add some of the headers from the source page
                    int headerCount = headers.Count;
                    for(int i =0; i < headerCount; i++)
                    {
                        Console.Out.WriteLine("Adding Header = "+ headers.GetKey(i));
                        if(headers.GetKey(i).ToLower().Equals("content-type"))
                        {
                            HttpWReq.ContentType= headers.Get(i);
                        }
                    }
 
                    byte[] data = getDataFromStream(ref reader);
                    HttpWReq.ContentLength= data.Length;
                    Stream requestStream =HttpWReq.GetRequestStream();
                   
                    //Write the data from the source
                    requestStream.Write(data,0, data.Length);
                   
                    //get the response (this should be HTTP 200)
                    HttpWebResponseHttpWRes=(HttpWebResponse)HttpWReq.GetResponse();
                   
                    if(HttpWRes.StatusCode==HttpStatusCode.OK)
                    {
                        //"Pushed";
                    }
                    else
                    {
                        //"Push Failed, bad response";
                    }
                }
                catch
                {
 
                }
                finally
                {
                    reader.Close();
                    requestStream.Close();
                    HttpWRes.Close();
                }
            }
            else
            {
                reader.Close();
            }
        }
       
        /**
         * Retrieves the source page information, this information (the headers and the data) are
         * stored in the reference variables headers (for the http headers) and reader (a StreamReader)
         * containing the source data.
         */
        private bool getInputPage(String url,refWebHeaderCollection headers,refStreamReader reader)
        {
            try
            {
                HttpWebRequest contentRequest =(HttpWebRequest)WebRequest.Create(url);
                HttpWebResponse contentResponse =(HttpWebResponse)contentRequest.GetResponse();
                if(contentResponse.StatusCode==HttpStatusCode.OK)
                {
                    headers = contentResponse.Headers;
                    Stream responseStream = contentResponse.GetResponseStream();
                    reader =newStreamReader(responseStream);
                    returntrue;
                }
                returnfalse;
            }
            catch
            {
                //"Could Not retrive Source Page";
                returnfalse;
            }
        }
        /**
         * Converts the data from a StreamReader into a byte[]
         */
        privatebyte[] getDataFromStream(refStreamReader reader)
        {
            StringBuilder sb =newStringBuilder();
            while(reader.Peek()>0)
            {
                sb.Append((char)reader.Read());
            }
            returnnewASCIIEncoding().GetBytes(sb.ToString());
 
        }
    }
}