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
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

Yes, both are running on completely different networks. I do not have network details. What details do you want?
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

I don't need details, but as this appears to be a network issue you will need to investigate why this is failing. 

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

But how about the other method that I posted Pushtest() which gives an "OK" response but the device is not receiving the message. The code is below:

 

private void Pushtest()
    {
        HttpWebResponse HttpWRes = null;
        HttpWebRequest HttpWReq = null;
        string pin = "xxxxxxxx"; // or actual pin of device
        string applicationID = "xxxx-xxxxxxxxxxxxxx";
        string BOUNDARY = "mPsbVQo0a68eIL3OAxnm";
        string msg = "testing c#"; // the message to send

        string userName = "xxxx-xxxxxxxxxxxxxx";
        string userPW = "xxxxxxxxxxx";

        string url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";

        HttpWReq = (HttpWebRequest)WebRequest.Create(url);
        HttpWReq.Method = ("POST");
        HttpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";

        //HttpWReq.Credentials = new NetworkCredential(userName, userPW);
        string rawCredentials = string.Format("{0}:{1}", userName, userPW);
        HttpWReq.Headers.Add("Authorization",
            string.Format(
                "Basic {0}",
                Convert.ToBase64String(Encoding.UTF8.GetBytes(rawCredentials))));

        HttpWReq.PreAuthenticate = true;
        HttpWReq.ContentType = "multipart/related; boundary=" + BOUNDARY + "; type=application/xml";
        string DeliverBefore = DateTime.UtcNow.AddMinutes(5).ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
        Response.Write(DeliverBefore);
        StringBuilder dataToSend = new StringBuilder();

        dataToSend.AppendLine("--" + BOUNDARY);
        dataToSend.AppendLine("Content-Type: application/xml; charset=UTF-8");

        dataToSend.AppendLine("");
        dataToSend.AppendLine("<?xml version=\"1.0\"?>");
        dataToSend.AppendLine("<!DOCTYPE pap PUBLIC \"-//WAPFORUM//DTD PAP 2.1//EN\" \"http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd\">");
        dataToSend.AppendLine("<pap>");
        string myPushId = DateTime.Now.ToFileTime().ToString();
        dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" source-reference=\"" + applicationID + "\">");
        dataToSend.AppendLine("<address address-value=\"" + pin + "\"/>");
        dataToSend.AppendLine("<quality-of-service delivery-method=\"unconfirmed\"/>");
        dataToSend.AppendLine("</push-message>");
        dataToSend.AppendLine("</pap>");
        dataToSend.AppendLine("--" + BOUNDARY);

        dataToSend.AppendLine("Content-Type: text/plain");
        dataToSend.AppendLine("Push-Message-ID: " + myPushId);
        dataToSend.AppendLine("");

        dataToSend.AppendLine(msg);

        dataToSend.AppendLine("--" + BOUNDARY + "--");
        dataToSend.AppendLine("");

        Stream requestStream = null;
        string pushResult = "";
        try
        {
            requestStream = HttpWReq.GetRequestStream();
        }
        catch (Exception ex)
        {
            pushResult = "Push failed! " + ex.ToString();
        }
        byte[] outStr = new ASCIIEncoding().GetBytes(dataToSend.ToString());
        requestStream.Write(outStr, 0, outStr.Length);
        requestStream.Close();

        try
        {
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
        }
        catch (Exception ex)
        {
            //push failed
        }

        if (HttpWRes != null)
        {
            HttpWRes.Close();
        }
    }

 I am struggling with this for months and I am totally lost. If its a network issue, what are the places to look into? Any tutorial that would help? I seriously need help and resolve this issue.

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

What is the full HTTP response received? The payload should be XML and provide some details on whether the push was accepted. If this is working then we should not need to worry about investigating networks, the proxy code being used may have been causing that issue.

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

When I run this method, the response returned is status code = "OK" from the following line of code:

 

try
        {
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
        }
        catch (Exception ex)
        {
            //push failed
        }

 The line before the above code returns:

  1. Message = "This stream does not support seek operations."

     

  2. Length = 'requestStream.Length' threw an exception of type 'System.NotSupportedException'
try
        {
            requestStream = HttpWReq.GetRequestStream();
        }
        catch (Exception ex)
        {
            pushResult = "Push failed! " + ex.ToString();
        }

 However, it does not go into the catch loops. requestStream has the above messages displayed when I run it in debug mode.

 

 

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

You need to read the response stream. At this point we are only dealing with standard C# networking, nothing related to BlackBerry Push.

 

I found this guide on the net, may be helpful:

http://www.codeproject.com/Articles/18034/HttpWebRequest-Response-in-a-Nutshell-Part-1

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

In that case, I believe the error is here since the response stream gives this data&colon;

Length = '((System.Net.ConnectStream)(requestStream)).Length' threw an exception of type 'System.NotSupportedException'

I am not getting back anything for the response stream.
Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

You should be using 

HttpWRes 

to read the response.

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.
Contributor
sarahfarukh
Posts: 40
Registered: ‎06-25-2012
My Device: BlackBerry Curver 9300
My Carrier: Etisalat

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

In order to read the respose I added the following line of code:

 

try
        {
            HttpWRes = (HttpWebResponse)HttpWReq.GetResponse();
            Stream Answer = HttpWRes.GetResponseStream();
        }
        catch (Exception ex)
        {
            //push failed
        }

 but 'Answer' returns 

Message = "This stream does not support seek operations."

Length = 'requestStream.Length' threw an exception of type 'System.NotSupportedException'

Please use plain text.
BlackBerry Development Advisor (Retired)
gbeukeboom
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: How Send Push Notifcation Using C#.Net Code In Blackberry

We're getting far out of the realm of BlackBerry development and deep into C# networking. 

 

I searched the web with the error you posted and the first result has a possible solution:

http://stackoverflow.com/questions/3434007/error-this-stream-does-not-support-seek-operations-in-c-s...

Garett
@garettBeuk
--
Goodbye everybody!
Please use plain text.