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
Highlighted
Developer
Posts: 16
Registered: ‎12-09-2012
My Device: BlackBerry Z10
My Carrier: Umniah
Accepted Solution

BB10 Push notification: PushPayLoad

I'm developing a C# application that can send push notification to BB10 devices using Blackberry Internet Service,I've found a code that it's working fine,however I don't know how to add custom data to the push,data that can be access with the push like pushPayLoad and to be used i.e. when user click on the push notification it will run my bb10 native application then navigate to user window or open any other window as bussiness required

 

here is my C# code:

 

HttpWebResponse HttpWRes = null;
            HttpWebRequest HttpWReq = null;
 
            string pin = "push_all";
            string applicationID = "ApplicationID";
            string BOUNDARY = "ASDFaslkdfjasfaSfdasfhpoiurwqrwm";
            string msg = "Good morning"// the message to send
 
            string userName = "ApplicationID";//"YOUR_APPLICATION_ID";
            string userPW = "YourPassword";//"YOUR_PASSWORD";
 
            string url = "https://pushapi.eval.blackberry.com/mss/PD_pushRequest";
 

 
            HttpWReq = (HttpWebRequest)WebRequest.Create(url);
 
            //SetProxy(HttpWReq); //if proxy needed, use this
 
            HttpWReq.Method = ("POST");
            HttpWReq.Accept = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2";
            HttpWReq.KeepAlive = true;
            //HttpWReq.Credentials = new NetworkCredential(userName, userPW);
 
            HttpWReq.PreAuthenticate = true;
            HttpWReq.ContentType = "multipart/related; boundary=" + BOUNDARY + "; type=application/xml";
 
            SetBasicAuthHeader(HttpWReq, userName, userPW);
            StringBuilder dataToSend = new StringBuilder();
 
            dataToSend.AppendLine("--" + BOUNDARY);
            dataToSend.AppendLine("Content-Type: application/xml; charset=UTF-8");
 
            
            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();
            string deliverBefore = DateTime.UtcNow.AddMinutes(5).ToString("s", System.Globalization.CultureInfo.InvariantCulture) + "Z";
            dataToSend.AppendLine("<push-message push-id=\"" + myPushId + "\" deliver-before-timestamp=\"" + deliverBefore + "\" 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 UTF8Encoding().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();
            }

 

 

What are the changes that I must do to the code to send data along with the push like profileId=123 ?

Developer
Posts: 16
Registered: ‎12-09-2012
My Device: BlackBerry Z10
My Carrier: Umniah

Re: BB10 Push notification: PushPayLoad

After a lot of tries and errors I finally found out the answer: before adding "msg" I can add as many payload items as I want :

 

    MyStringBuilder.AppendLine("profileId" + ":" + "AAB123");

    MyStringBuilder.AppendLine("postId" + ":" + "4567");

    MyStringBuilder.AppendLine(msg);

 

Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: BB10 Push notification: PushPayLoad

Glad to hear you figured things out. Smiley Happy

Developer
Posts: 16
Registered: ‎12-09-2012
My Device: BlackBerry Z10
My Carrier: Umniah

Re: BB10 Push notification: PushPayLoad

It would be nice if there is a sample code on how to send image push notification,I can't find any sample on this,I'm working on PushSharp Library and we're doing our best to add support to blackberry and be available to every .net developer,any help on this regard is much appreciated

Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: BB10 Push notification: PushPayLoad

Our server-side samples (the low-level sample Push Initiator and high-level sample Push Initiator) allow you to push images.

And, our sample push-enabled applications that run on the BlackBerry show how to display pushed images.

 

One thing to note about pushing images though is that the limit is 8 KB.

 

You would have to use a poke-and-pull pattern if you wanted to send larger than this size.  Basically, you would send a push with a URL to your server and the app would then use that URL in the push content to go and download the image stored on your server.

 

Matt