09-07-2012 01:19 PM - edited 09-07-2012 03:09 PM
hi there, i have a code that recive push and p the diplay a dialog and put icon on de notifications banner, when i close the app i put it in background implementing onClose() in main screen, after this i send a push from server but the notification icon is visible when i open the app again (foreground), and now i implement the same app with entry point that means i start the app in background then when i send push is the same issue, and i hear some about globalListener to comincate the app in background with the UI app, maybe this solve the problem??.
some code:
public class MyApp extends UiApplication
{
/**
* Entry point for application
* @param args Command line arguments (not used)
*/
public static void main(String[] args) {
if ( args != null && args.length > 0 && args[0].equals("gui") ){
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
} else {
PushAgent push = new PushAgent();
}
}
Solved! Go to Solution.
09-10-2012 04:20 AM
09-10-2012 06:17 PM
ok, well i just implemented the GlobalPushListener in my "brackgound App" ( is the pushAgent class ) and look like this
public void eventOccurred(long guid, int data0, int data1, Object object0,
Object object1) {
if( ! ( guid == ConstanGUID.GUID) ){
return;
}
ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
EncodedImage image = EncodedImage.getEncodedImageResource("new_notifica tion.jpg");
ApplicationIcon icon = new ApplicationIcon( image );
ApplicationIndicator indicator = reg.register( icon, false, true);
indicator.setIcon(icon);
indicator.setVisible(true);
}
then in the process method of the class PushMessageReader.java i make the call of ApplicationManager.getApplicationManager().post
that method look like this:
public static void process(PushInputStream pis, Connection conn) {
System.out.println("Reading incoming push message ...");
dialogo("Reading incoming push message ...");
try {
HttpServerConnection httpConn;
if (conn instanceof HttpServerConnection) {
httpConn = (HttpServerConnection) conn;
} else {
dialogo("Can not process non-http pushes, expected HttpServerConnection but have "
+ conn.getClass().getName());
throw new IllegalArgumentException("Can not process non-http pushes, expected HttpServerConnection but have "
+ conn.getClass().getName());
}
String msgId = httpConn.getHeaderField(MESSAGE_ID_HEADER);
String msgType = httpConn.getType();
String encoding = httpConn.toString();
inputStream = httpConn.openInputStream();
byte[] data = IOUtilities.streamToBytes(inputStream);
String result = new String(data,"UTF-8");
dialogo("result " + result );
System.out.println("Message props: ID=" + msgId + ", Type=" + msgType + ", Encoding=" + encoding);
dialogo("Message props: ID=" + msgId + ", Type=" + msgType + ", Encoding=" + encoding);
boolean accept = true;
if (!alreadyReceived(msgId)) {
byte[] binaryData;
if (msgId == null) {
msgId = String.valueOf(System.currentTimeMillis());
}
if (msgType == null) {
dialogo("Message content type is NULL");
System.out.println("Message content type is NULL");
accept = false;
} else if (msgType.indexOf(MESSAGE_TYPE_TEXT) >= 0) {
// a string
byte[] data2 = IOUtilities.streamToBytes(inputStream);
String result2 = new String(data2,"UTF-8");
ApplicationManager.getApplicationManager().postGlo balEvent(ConstanGUID.GUID);
// TODO report message
} else if (msgType.indexOf(MESSAGE_TYPE_IMAGE) >= 0) {
// an image in binary or Base64 encoding
int size = pis.read(buffer);
if (encoding != null && encoding.equalsIgnoreCase("base64")) {
// image is in Base64 encoding, decode it
Base64InputStream bis = new Base64InputStream(new ByteArrayInputStream(buffer, 0, size));
size = bis.read(imageBuffer);
}
binaryData = new byte[size];
System.arraycopy(buffer, 0, binaryData, 0, size);
// TODO report message
} else {
System.out.println("Unknown message type " + msgType);
accept = false;
}
} else {
System.out.println("Received duplicate message with ID " + msgId);
dialogo("Received duplicate message with ID " + msgId);
}
pis.accept();
} catch (Exception e) {
System.out.println("Failed to process push message: " + e);
dialogo("Failed to process push message: " + e);
} finally {
PushAgent.close(conn, pis, null);
}
}
finally in the main class check if there is a gui entry or not (background) and then i registerd the listener
public static void main(String[] args) {
if (args != null && args.length > 0 && args[0].equals("gui")) {
MyApp theApp = new MyApp();
theApp.enterEventDispatcher();
} else {
PushAgent app = PushAgent.getInstance();
app.invokeLater(new Runnable() {
public void run() {
ApplicationManager appManager = ApplicationManager
.getApplicationManager();
// sleep until the device is started
while (appManager.inStartup()) {
try {
Thread.sleep(500);
} catch (Throwable error) {
// nothing
}
}
PushAgent.getInstance().initialize();
}
});
app.enterEventDispatcher();
}
}
and nothing hapent
i mean i want implement push like other apps(whatsapp bbmsn ...), is this the way right?
09-11-2012 03:25 AM
09-11-2012 04:53 PM
THX for the advice, i just did what u said, postglobalEvent in the background app when the push is comming in the process method of below, in this part
else if (msgType.indexOf(MESSAGE_TYPE_TEXT) >= 0) {
// a string
byte[] data2 = IOUtilities.streamToBytes(inputStream);
String pushText = new String(data2,"UTF-8");
ApplicationManager.getApplicationManager().postGlo balEvent(ConstanGUID.GUID,0, 0,pushText,null);
//Application.getApplication().requestForeground() ;
//Dialogo.show("lego el push");
}
( btw i know the push is coming if u see the 2 last lines of code above when i descomenter i can read the push,)
then implment the GlobalEventListener interface in my main app as u can see below
public class UiApp extends UiApplication implements GlobalEventListener{
private static PushAgent pushAgentApp;
private static UiApp UiApp;
public static void main(String[] args) {
if (args != null && args.length > 0 && args[0].equals("guii")) {
UiApp = new UiApp();
UiApp.enterEventDispatcher();
} else {
pushAgentApp = PushAgent.getInstance();
ApplicationManager appManager = ApplicationManager
.getApplicationManager();
// sleep until the device is started
while (appManager.inStartup()) {
try {
Thread.sleep(500);
} catch (Throwable error) {
// nothing
}
}
pushAgentApp.enterEventDispatcher();
}
}
/**
* Creates a new MyApp object
*/
public UiApp() {
// Push a screen onto the UI stack for rendering.
pushScreen(new MyScreen());
}
//handle events
public void eventOccurred(long guid, int data0, int data1, Object object0,
Object object1) {
Dialogo.show("Event ocurr");
if( ! ( guid == ConstanGUID.GUID) ){
Dialogo.show("Not constans GUID");
return;
}
Dialogo.show("the F**k GUID");
ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
EncodedImage image = EncodedImage.getEncodedImageResource("new_notifica tion.jpg");
ApplicationIcon icon = new ApplicationIcon( image );
ApplicationIndicator indicator = reg.register( icon, false, true);
indicator.setIcon(icon);
indicator.setVisible(true);
}
but the listeners never get fire
, it looks simple but OMG what i am doing wrong? help me ![]()
this is the constant that i use for the event is long number what i put arbitrarily
public class ConstanGUID{
public final static long GUID = 919928183;
}
09-12-2012 02:01 AM - edited 09-12-2012 02:02 AM
i think you are forgetting to register the global event listener, use http://www.blackberry.com/developers/docs/7.1.0api
also be aware of the possibility that your UI part may not be running at the time, you can check that using http://www.blackberry.com/developers/docs/7.1.0api
if this is the case, you could
- launch the UI using applicationmanger, then send the event
- launch the UI using an alternate entry point with the push data
- display something from the background thread (keyword global screen)
- do something else ![]()
regarding the GUID: I would suggest that you use a string, for example the fully qualified name of the class, and convert it to long using the eclipse plugin.
09-12-2012 10:48 AM
ok now i reg the listener and checking if the uipart is alive before post the global Event, but i wonder how i will achieve the notification banner get update beacuse i dont want push any screens when i start the UIApp (i want do it like bbmsn push or whatsapp) if there is not started
09-12-2012 11:11 AM
09-12-2012 11:18 AM
after i did all what u said in the last post i finally get the globalEventListener Fired, but i still wondering how achieve update the notification banner i and almost of finish my first version of the app help me with this
, i just read this in stackOverflow (if there is true i will search some examples but if u can tell me where i should focus would be great
) :
Create a separate app (or give your existing app a second entry point) that implementsnet.rim.device.api.system.Application and is configured to run at device startup. That instance of your app will then be running in its own background process for as long as the OS is running. It can do whatever data polling it needs to do, and then display notifications to the user when needed, such as with the net.rim.blackberry.api.messagelist.ApplicationIndi and net.rim.blackberry.api.messagelist.ApplicationMess APIs, or your own custom UI.
09-12-2012 11:21 AM
yest that small indicator icon on the homescreen, let me make a couple of test because i was updating that the small indicator icon on the homescreen as backgound in 1 main app, now i have 2 app the UI and the push Listener, i will try update the nitification banner in my push listener and will share here the results