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

Web and WebWorks Development

Reply
Contributor
wtdnielsen
Posts: 30
Registered: ‎09-19-2013
My Device: None

Receive push while app is closed

Hello. I know this question has been asked before but I have not found an answer. I have developed an app that receives push messages. This works as long as the app is open or has been closed for max 5 minutes. If it has been closed for 10 minutes or more the app stops receiving push messages.
Here is my code:
.NET
[code]
String httpUrl = "http://" + BesServer
+ ":" + BesPushPort
+ "/push" + destinations
+ "&PORT=" + "mytestapp"
+ "&REQUESTURI=/";

var msg = "hello";
var httpWReq = (HttpWebRequest)WebRequest.Create(httpUrl);
httpWReq.Method = ("POST");
httpWReq.ContentType = "text/plain";
byte[] data = ASCIIEncoding.UTF8.GetBytes(msg);
if (data.Length <= 8192)
{
httpWReq.ContentLength = data.Length;
Stream requestStream = httpWReq.GetRequestStream();
requestStream.Write(data, 0, data.Length);
var httpWRes = (HttpWebResponse)httpWReq.GetResponse();
if (httpWRes.StatusCode == HttpStatusCode.OK)
{
//write success push
_pushDataLayer.Insert(msg);

}
else
{
//write fail push

}
requestStream.Close();
httpWRes.Close();
}
[/code]

Config.xml
[code]
<widget xmlns:rim="http://www.blackberry.com/ns/widgets" version="1.0.0.2" rim:header="RIM-Widget:rim/widget" xmlns="http://www.w3.org/ns/widgets" id="mytestapp">
<name>mytestapp</name>
<description>testing</description>
<author >Magic</author>
<icon src="images/pushCaptureIcon.png" />
<content src="index.htm" />

<!-- Need to allow access to your externally accessible Push Initiator URL if using the Push Service SDK high-level APIs (with subscription support) -->
<!-- Replace "externally_accessible_Push_Initiator_URL" with your own domain name -->
<access uri="http://myserver:89" subdomains="true" />

<feature id="blackberry.app">
<param name="orientation" value="portrait" />
</feature>
<feature id="blackberry.app" />
<feature id="blackberry.push" />
<feature id="blackberry.system" />
<feature id="blackberry.invoked" />
<feature id="blackberry.ui.dialog" />
<feature id="blackberry.ui.toast" />

<feature id="blackberry.identity" />
<feature id="blackberry.invoke" />

<rim:smileytongue:ermissions>
<rim:smileytongue:ermit>post_notification</rim:smileytongue:ermit>
<!-- This permission is needed for a consumer (public) push application only (not enterprise push)! -->
<!--<rim:smileytongue:ermit system="true">_sys_use_consumer_push</rim:smileytongue:ermit>-->
<rim:smileytongue:ermit>read_device_identifying_information</rim:smileytongue:ermit>
</rim:smileytongue:ermissions>

<!-- Need to put an invoke entry here for push -->
<!-- The id here must match the invokeTargetId passed in as one of the ops to blackberry.push.PushService.create -->
<rim:invoke-target id="mytestapp.invoke.push">
<type>APPLICATION</type>
<filter>
<action>bb.action.PUSH</action>
<mime-type>application/vnd.push</mime-type>
</filter>
</rim:invoke-target>

<!-- Have an invoke entry here for when a notification is clicked on in the BlackBerry Hub -->
<!-- This will cause the application to be opened -->
<rim:invoke-target id="mytestapp.invoke.open">
<type>APPLICATION</type>
<filter>
<action>bb.action.OPEN</action>
<mime-type>text/plain</mime-type>
</filter>
</rim:invoke-target>
</widget>

[/code]
JavaScript&colon;
[code]
PushCapture.prototype.initPushService = function () {
if (blackberry.push) {
blackberry.event.addEventListener("invoked", sample.pushcapture.onInvoke);
if (blackberry.app.windowState == "fullscreen") {
sample.pushcapture.hasBeenInForeground = true;
}

blackberry.event.addEventListener("resume", sample.pushcapture.onResume);
sample.pushcapture.createPushService();
}
};
PushCapture.prototype.createPushService = function () {
var ops = {
invokeTargetId: "mytestapp.invoke.push",
appId: "mytestapp"
};

blackberry.push.PushService
.create(ops, sample.pushcapture.successCreatePushService, sample.pushcapture.failCreatePushService,
sample.pushcapture.onSimChange, sample.pushcapture.onPushTransportReady);
};

PushCapture.prototype.onInvoke = function (invokeRequest) {
if (invokeRequest.action != null && invokeRequest.action == "bb.action.PUSH") {
var pushPayload = sample.pushcapture.pushService.extractPushPayload(invokeRequest);
sample.pushcapture.pushNotificationHandler(pushPayload);
}
};
PushCapture.prototype.successCreatePushService = function (service) {
sample.pushcapture.pushService = service;
sample.pushcapture.pushService.launchApplicationOnPush(true,
sample.pushcapture.launchApplicationCallback);

};
PushCapture.prototype.launchApplicationCallback = function (result) {
if (sample.pushcapture.pushService) {
sample.pushcapture.pushService.createChannel(sample.pushcapture.createChannelCallback);
}
});
PushCapture.prototype.createChannelCallback = function (result, token) {
});
sample.pushcapture.constructor.prototype.pushNotificationHandler = function (pushpayload) {
var contentType = pushpayload.headers["Content-Type"];
sample.pushcapture.processPush(pushpayload.data, contentType);
if (pushpayload.isAcknowledgeRequired) {
pushpayload.acknowledge(true);
}
};
sample.pushcapture.constructor.prototype.processPush = function (content, contentType) {
};
[/code]

In my index.htm header I have this code:
[code]
<script type="text/javascript">

function ready() {


/* Register our BlackBerry Push Service. */
// Create the push service (i.e. call create, etc.)
sample.pushcapture.initPushService();

}


window.addEventListener("load", function (e) {
document.addEventListener("webworksready", ready);
}, false);


</script>
[/code]

Please use plain text.
Contributor
wtdnielsen
Posts: 30
Registered: ‎09-19-2013
My Device: None

Re: Receive push while app is closed

I guess this is the problem:

http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/OS-10-2-0-424-The-app-is-unable-t...

 

My app is installed in Workspace with password protection and It is not possible to receive push messages while Workspace is locked. The only solution so far is to write a native version instead :/ 

is this true?

Please use plain text.