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

Sample Code of BB 10 Push Notification in 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.

 

Looks like icon push is not supported by BES10/BB10. Is there anyway we can get this done in BES10

 

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

 

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());
 
 
        }
 
    }
 
}

 

Can anybody help on this.

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10

Re: Sample Code of BB 10 Push Notification in c#

BlackBerry Channel Push is not supported on BlackBerry 10. You would need to write a client and new server-side application to accomplish the same functionality as seen in BlackBerry OS 7 and lower.

Garett
@garettBeuk
--
Goodbye everybody!
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10

Re: Sample Code of BB 10 Push Notification in c#

Can you please share  some sample code or direct me some website to look on Client and server. ?

 

 

_ajait

 

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10

Re: Sample Code of BB 10 Push Notification in c#

Client and server samples can be found here:

https://github.com/blackberry/BES10-Cascades/tree/master/ServerNotify

 

These will show how to handle push messages, but you will need to add logic to have the app perform a Browser Channel Push-like behaviour.

Garett
@garettBeuk
--
Goodbye everybody!
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10

Re: Sample Code of BB 10 Push Notification in c#

So we can confrim that above pasted code won't work in BES10.

 

Thanks for your help.

 

i will look into that examples and let you know if something help required from yourself.

 

_ajait

Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10

Re: Sample Code of BB 10 Push Notification in c#

i have downloaded the client files https://github.com/blackberry/BES10-Cascades/tree/master/ServerNotify from this link. and also downlaoded Momentics IDE for blackberry Native. i tried to import the dowladed files to workspace.

 

SimlpePushClient project loaded in Momentics

 

But im getting below error when we build the project

 

1. Program 'make' not found in the PATH

1. Program 'qcc' not found in the PATH

 

Please help

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10

Re: Sample Code of BB 10 Push Notification in c#

How did you download the project (zip, git etc)?

What steps did you take to import the project?

Garett
@garettBeuk
--
Goodbye everybody!
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10

Re: Sample Code of BB 10 Push Notification in c#

i downloaded from the site as  'Downlaod Zip' and extracted the zip files.

 

After that i opened the Momnetic IDE and in Project Exploere --> Right Click Import --> Existing Project intp WorkSpce-->Select root directory ---> Pointed the extratced folder till Client --> Server Notify

 

its loaded SamplePushClient ?

 

 

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10

Re: Sample Code of BB 10 Push Notification in c#

The name could be different, I think this sample is based off another with the name SimplePushClient.

 

How are you launching Momentics? Can you try creating and then building a sample app to see if it works as expected?

Garett
@garettBeuk
--
Goodbye everybody!
Contributor
Posts: 10
Registered: ‎02-12-2014
My Device: BlackBerry 10

Re: Sample Code of BB 10 Push Notification in c#

Yes example only i'm trying it  and to understand.

 

I have downloaded Momentics IDE and installed in my PC. i will try some sample app.

 

Also please let know how this lclient app will help  in notiifcations.

 

 

thnks,

_ajait