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

Java Development

Reply
Highlighted
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet
Accepted Solution

Push Background/Foreground issue

[ Edited ]

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

 

 

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Push Background/Foreground issue

you can add a http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/GlobalEventListener.htm... to an application, and use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/ApplicationManager.html... (and similar) to post events.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Push Background/Foreground issue

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_notification.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().postGlobalEvent(ConstanGUID.GUID);

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().postGlobalEvent(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? 

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Push Background/Foreground issue

your background app is connected with the push listener, correct?
then you have to send the event from there, and receive it in your main application.
btw, don't use invokelater for the initial sleep, that makes no sense.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Push Background/Foreground issue

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().postGlobalEvent(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_notification.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 Smiley Frustrated , it looks simple but OMG what i am doing wrong? help me Smiley Wink

 

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

 

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Push Background/Foreground issue

[ Edited ]

i think you are forgetting to register the global event listener, use http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/system/Application.html#addGlo...

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/net/rim/device/api/system/ApplicationManager.html...
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 Smiley Wink

 

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.

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Push Background/Foreground issue

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

Developer
Posts: 16,984
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Push Background/Foreground issue

what do you mean with "notification banner"? the small indicator icon on the homescreen?
you can control that with http://www.blackberry.com/developers/docs/7.1.0api/net/rim/blackberry/api/messagelist/ApplicationInd...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Push Background/Foreground issue

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 Smiley Very Happy, 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 Smiley Wink) :

 

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.ApplicationIndicator and net.rim.blackberry.api.messagelist.ApplicationMessageFolder APIs, or your own custom UI.

 

 

Developer
Posts: 90
Registered: ‎04-03-2012
My Device: 9100
My Carrier: movilnet

Re: Push Background/Foreground issue

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