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
Developer
Posts: 172
Registered: ‎05-01-2010
My Device: Z10
My Carrier: O2
Accepted Solution

Webworks Push Client

Hi,

 

I am somehow stuck in the logical process to receive a push message in a webworks application. The example application is working fine and I can receive messages with it. I guess that I am understanding something wrong.

 

What I understand so far is:

 

1. create pushservice

2. create event listener

3. enable application to launch on push

4. create channel

 

and everything should be fine... or not? Lets talk code:

 

config.xml (part)

<rim:permissions>
	<rim:permit>post_notification</rim:permit>
	<rim:permit system="true">_sys_use_consumer_push</rim:permit>
	<rim:permit>read_device_identifying_information</rim:permit> 
</rim:permissions> 
<rim:invoke-target id="bert.pushcapture.invoke.push">      
	<type>APPLICATION</type>      
	<filter>
		<action>bb.action.PUSH</action>
   		<mime-type>application/vnd.push</mime-type>      
	</filter>
</rim:invoke-target>

 javascript&colon;

function createPushService() {
  try {

   		ops = { invokeTargetId : 'bert.pushcapture.invoke.push', 
   				appId : 'XXX-XXXX...', 
   				ppgUrl : 'http://cpXXX.pushapi.na.blackberry.com'
		};
  	
    	blackberry.push.PushService.create(ops, successCreatePushService, failCreatePushService, onSimChange, onPushTransportReady);  

  }
  catch (err) {
    alert(err);
  }     
} 



function successCreatePushService(service) {
	//alert("Created Push service");
	// check this **bleep**
	pushService = service;
   	blackberry.event.addEventListener("invoked", onInvoke);	
	launchApplicationOnPush(true, launchApplicationCallback);
}

function failCreatePushService(result) {
	alert("Error: Received error code (" + result + ") after " + "calling blackberry.push.PushService.create.");
}

function onPushTransportReady(result) {
	if (result == blackberry.push.PushService.SUCCESS) {
    			alert("successful Configuration");
    	} else {    		
    		
	    	if (result == blackberry.push.PushService.INTERNAL_ERROR) {
				alert("Error: An internal error occurred while calling launchApplicationOnPush. " + "Try restarting the application.");
	    	} else if (result == blackberry.push.PushService.CREATE_SESSION_NOT_DONE) {
				alert("Error: Called launchApplicationOnPush without an " +
				"existing session. It usually means a programming error.");
	    	} else {
				alert("Error: Received error code (" + result + ") after " + "calling launchApplicationOnPush."); 		
	    	}
		}
}

function launchApplicationCallback(result) {
	if (result == blackberry.push.PushService.SUCCESS) {
    			alert("successful Configuration");
    	} else {
    		
	    	if (result == blackberry.push.PushService.INTERNAL_ERROR) {
				alert("Error: An internal error occurred while calling launchApplicationOnPush. " + "Try restarting the application.");
	    	} else if (result == blackberry.push.PushService.CREATE_SESSION_NOT_DONE) {
				alert("Error: Called launchApplicationOnPush without an " + "existing session. It usually means a programming error.");
	    	} else {
				alert("Error: Received error code (" + result + ") after " + "calling launchApplicationOnPush."); 		
	    	}
    	}	
}

function onSimChange() {
	alert("SIM card is changed!");
}


function createChannelCallback(result, token) {
    if (result === blackberry.push.PushService.SUCCESS) {
        alert("channel created");
    } else if (result === blackberry.push.PushService.INTERNAL_ERROR) {
        alert("channel failed");
        
    }
}



function onInvoke (invokeRequest) {    	
    	if (invokeRequest.action != null && invokeRequest.action == "bb.action.PUSH") {
    		if (pushService == null) {    			
    			setTimeout(function() { onInvoke(invokeRequest); }, 750);
    		} else {
            	var pushPayload = extractPushPayload(invokeRequest);            	
            	pushNotificationHandler(pushPayload);
    		}
    	} 
}
    
function pushNotificationHandler (pushpayload) {	
    var contentType = pushpayload.headers["Content-Type"];
    if(!contentType) {
    	contentType = pushpayload.headers["content-type"];
    	if(!contentType) {
    		contentType = "text/plain";
    	}
    }
    
	db.transaction(
		function(tx) {
			tx.executeSql("INSERT INTO Data (title) values(?)", [ "PUSH"], null);
		});
	
    //alert("PUSH RECEIVED");
    pushpayload.acknowledge(true);

};


I am calling following functions through a button:

 

1. createPushService();

2. pushService.createChannel(createChannelCallback);

 

and I receive an "alert("channel created");" and this means to me that the PushService and the Channel was created successfully.

 

Since I create a event for a "invoked" via "blackberry.event.addEventListener("invoked", onInvoke);" the function "onInvoke" should be called when a new push is received. Correct?

 

Unfortunately it is not working and I dont get any response or error.

 

Any help or hint is appreciated. I am really stuck here...

 

best

 

bert2002

 



----------------------------------------------------------
find my widgets -> http://blackberry.itbert.de
Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: Webworks Push Client

Hi,

 

A couple of things to ask you about.  Not sure if they're related to your problem, but it's worth a shot. 

 

1. You call extractPushPayload below when processing a push invoke.  But, where is this function defined?  You should be calling pushSevice.extractPushPayload somewhere.

2. You shouldn't need the setTimeout function anymore if you add the event listener for the "invoked" event in your blackberry.push.PushService.create success callback.  Our sample no longer uses setTimeout.

3. In your code right now, you are calling launchApplicationOnPush and createChannel everytime.  You only really need to call them once.  After you get successful launchApplicationonPush and createChannel callbacks the first time, then you're good to receive pushes.  You would only need to do it again if you do a destroyChannel somewhere.  You do still need to call blackberry.push.PushService.create everytime though.

4. Are you defining the <feature id="blackberry.invoked" /> tag in your config.xml?

 

Hope that helps,

 

Matt

Developer
Posts: 172
Registered: ‎05-01-2010
My Device: Z10
My Carrier: O2

Re: Webworks Push Client

[ Edited ]

Yeah, you got it. I missed it creating extractPushPayload. Now its working. That is amazing. Thank you soo much. Somehow I missed that :smileyhappy:

 

ONE more question. Is the channel deleted when I remove the application?



----------------------------------------------------------
find my widgets -> http://blackberry.itbert.de
Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: Webworks Push Client

Great!  Yep, the channel will be destroyed automatically when the app is removed.

Developer
Posts: 172
Registered: ‎05-01-2010
My Device: Z10
My Carrier: O2

Re: Webworks Push Client

Ohh thats nice to know. I guess its not possible to check if a channel exist already (yet :smileyhappy: ?



----------------------------------------------------------
find my widgets -> http://blackberry.itbert.de
Retired
Posts: 311
Registered: ‎08-08-2012
My Device: BlackBerry Z10
My Carrier: Rogers

Re: Webworks Push Client

You mean from your application?  I would recommend recording in a database, localStorage, etc. whether or not you did a successful createChannel so that you don't have to call it repeatedly.

New Contributor
Posts: 2
Registered: ‎03-26-2013
My Device: MOBILE
My Carrier: BLACKBERRY

Re: Webworks Push Client

HEY GUYS,HOW CAN CLIENT   HANDLE WITH SERVER?