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
New Contributor
drinkwater
Posts: 8
Registered: ‎05-30-2010
My Device: Curve

My contribution to this forum

Based on several posts on this forum, I have come up with the following in VB.NET code to send Push requests.

 

Enjoy...

 

HttpBBPushRequest class :


 

Imports System.Net
Imports System.IO
Imports System.Web
Imports System.Text
Imports System.Xml

Public Class HttpBBPushRequest
    Private _Url As String = ""
    Private _ApplicationID As String = ""
    Private _UserName As String = ""
    Private _UserPassword As String = ""

    Private _StatusCode As String = ""
    Private _StatusDescription As String = ""
    Private _Response As String = ""
    Private _PushResultCode As String = ""
    Private _PushResultDesc As String = ""

    Public Sub New()

    End Sub

    Public Property ApplicationID() As String
        Get
            Return _ApplicationID
        End Get
        Set(ByVal value As String)
            _ApplicationID = value
        End Set
    End Property

    Public Property Url() As String
        Get
            Return _Url
        End Get
        Set(ByVal value As String)
            _Url = value
        End Set
    End Property

    Public Property UserName() As String
        Get
            Return _UserName
        End Get
        Set(ByVal value As String)
            _UserName = value
        End Set
    End Property

    Public Property UserPassword() As String
        Get
            Return _UserPassword
        End Get
        Set(ByVal value As String)
            _UserPassword = value
        End Set
    End Property

    Public ReadOnly Property StatusCode() As String
        Get
            Return _StatusCode
        End Get
    End Property

    Public ReadOnly Property StatusDescription() As String
        Get
            Return _StatusDescription
        End Get
    End Property

    Public ReadOnly Property Response() As String
        Get
            Return _Response
        End Get
    End Property

    Public ReadOnly Property PushResultCode() As String
        Get
            Return _PushResultCode
        End Get
    End Property

    Public ReadOnly Property PushResultDesc() As String
        Get
            Return _PushResultDesc
        End Get
    End Property

    Public Sub Push(ByVal PIN As String, ByVal Message As String, Optional ByVal ID As String = "")
        Dim Request As HttpWebRequest = Nothing
        Dim Response As HttpWebResponse = Nothing
        Dim Reader As StreamReader = Nothing
        Dim Boundary As String = "mPsbVQo0a68eIL3OAxnm"

        _StatusCode = ""
        _StatusDescription = ""

    	_PushResultCode = ""
    	_PushResultDesc = ""

        Request = DirectCast(WebRequest.Create(_Url), HttpWebRequest)
        Request.Method = "POST"
        Request.Timeout = System.Threading.Timeout.Infinite

        Request.PreAuthenticate = True
        Request.ContentType = "multipart/related; boundary=" & Boundary & "; type=application/xml"

        Dim AuthInfo As String = _UserName & ":" & _UserPassword
        AuthInfo = Convert.ToBase64String(Encoding.Default.GetBytes(AuthInfo))

        Request.Headers("Authorization") = "Basic " & AuthInfo

        Dim Data As StringBuilder = New StringBuilder()

        Dim DeliverBefore As String = DateTime.UtcNow.AddMinutes(5).ToString("s", System.Globalization.CultureInfo.InvariantCulture) & "Z"

        If ID = "" Then
            ID = DateTime.Now.ToFileTime().ToString()
        End If

        Data.AppendLine("--" & Boundary)
        Data.AppendLine("Content-Type: application/xml; charset=utf-8")
        Data.AppendLine("")
        Data.AppendLine("<?xml version=""1.0""?>")
        Data.AppendLine("<!DOCTYPE pap PUBLIC ""-//WAPFORUM//DTD PAP 2.1//EN"" ""http://www.openmobilealliance.org/tech/DTD/pap_2.1.dtd"">")
        Data.AppendLine("<pap>")
        Data.AppendLine("<push-message push-id=" & Chr(34) & ID & Chr(34) & " deliver-before-timestamp=" & Chr(34) & DeliverBefore & Chr(34) & " source-reference=" & Chr(34) & _ApplicationID & Chr(34) & ">")
        Data.AppendLine("<address address-value=""" & PIN & """/>")
        Data.AppendLine("<quality-of-service delivery-method=""unconfirmed""/>")
        Data.AppendLine("</push-message>")
        Data.AppendLine("</pap>")
        Data.AppendLine("--" & Boundary)

        Data.AppendLine("Content-Type: text/plain")
        Data.AppendLine("Push-Message-ID: " & ID)
        Data.AppendLine("")
        Data.AppendLine(Message)
        Data.AppendLine("--" & Boundary & "--")
        Data.AppendLine("")

        Try
            Dim PostBytes As Byte() = Encoding.Default.GetBytes(Data.ToString())

            Dim RequestStream As Stream = Request.GetRequestStream()
            RequestStream.Write(PostBytes, 0, PostBytes.Length)
            RequestStream.Close()

            Response = DirectCast(Request.GetResponse(), HttpWebResponse)
            Reader = New StreamReader(Response.GetResponseStream(), Encoding.UTF8)

            _Response = Reader.ReadToEnd
            _StatusCode = Response.StatusCode
            _StatusDescription = Response.StatusDescription

            If _StatusCode = "200" Then
                Dim XmlDoc As XmlDocument = New XmlDocument()

                XmlDoc.LoadXml(_Response)

                Dim Node As XmlNode = XmlDoc.SelectSingleNode("pap/push-response/response-result")
                If Not IsNothing(Node) Then
                    For I As Integer = 0 To Node.Attributes.Count - 1
                        If Node.Attributes(I).Name = "code" Then
                            _PushResultCode = Node.Attributes(I).Value
                        End If
                        If Node.Attributes(I).Name = "desc" Then
                            _PushResultDesc = Node.Attributes(I).Value
                        End If
                    Next
                End If
            End If

        Catch Ex As WebException
            If Not Ex.Response Is Nothing Then
                Dim ErrorResponse As HttpWebResponse = Nothing

                Try
                    ErrorResponse = DirectCast(Ex.Response, HttpWebResponse)
                    Reader = New StreamReader(ErrorResponse.GetResponseStream())

                    _Response = Reader.ReadToEnd()

                    _StatusCode = ErrorResponse.StatusCode
                    _StatusDescription = ErrorResponse.StatusDescription
                Finally
                    If Not ErrorResponse Is Nothing Then
                        ErrorResponse.Close()
                    End If
                End Try
            End If

        Catch Ex As Exception
            Throw

        Finally
            If Not Reader Is Nothing Then
                Reader.Close()
            End If

            If Not Response Is Nothing Then
                Response.Close()
            End If
        End Try
    End Sub
End Class

 

 

Sample Usage :

 

Dim BBPushRequest As New HttpBBPushRequest

BBPushRequest.Url = "http://pushapi.eval.blackberry.com/mss/PD_pushRequest"
BBPushRequest.ApplicationID = "your application id"
BBPushRequest.UserName = "your application id"
BBPushRequest.UserPassword = "your password"

Dim Message As String = "your push message content"
Dim PIN As String = "push_all" ' or one or more PIN numbers

BBPushRequest.Push(PIN, Message)

If BBPushRequest.PushResultCode = "1000" Or BBPushRequest.PushResultCode = "1001" Then
'Success
Else
'Failure
End If

 

 

 

 

Contributor
1llus1on
Posts: 19
Registered: ‎06-23-2010
My Device: Bold

Re: My contribution to this forum

Do you have a sample Push Initiator that write in PHP ?

 

Thanks

New Contributor
drinkwater
Posts: 8
Registered: ‎05-30-2010
My Device: Curve

Re: My contribution to this forum

No - I don't use PHP - but I believe there alot of PHP code snippets in the various posts on this forum.

New Contributor
vbextra
Posts: 4
Registered: ‎09-01-2010
My Device: Not Specified

Re: My contribution to this forum

Hi,

 

What is Authorization header and why it required? Where to get password?

 

Please help

 

regards

Developer
bb_cladsa
Posts: 58
Registered: ‎01-14-2010
My Device: 9000

Re: My contribution to this forum

Through the Blackberry Administration Service, the admin can configure applications which are allowed to send push messages to devices. Here is a link on how to configure it in the BES.

 

http://docs.blackberry.com/en/admin/deliverables/16606/Restricting_push_content_users_receive_on_dev...

Visitor
pfarquharson
Posts: 1
Registered: ‎09-17-2010
My Device: Not Specified

Re: My contribution to this forum

Hi,

 

I've been trying to use this code and I ran into some difficulty. Whenever I plug in the information provided to me and try to send a push request, I get the following error: "Root element is missing". I'm assuming that this is because no response was sent back to the Initiator. I'm wondering if you can point me in the right direction if you've ever run into this problem.

 

BTW, I'm using the Blackberry sample code for the client-side (in the widget).

Visitor
SahilKakkar
Posts: 1
Registered: ‎03-29-2012
My Device: bold

Re: My contribution to this forum

i have used this Code But i am Getting 400 bad request

Is it means i am Not connecting To remote server

Or m doing somthing wrong

Please Guide me to Right Direction ASAP