BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 11: Push

Push has always been a major part of BlackBerry as an extremely efficient way of providing real-time data to users without needing to keep your app running at all times. The main components of Push from BlackBerry 7 to BlackBerry 10 have remained largely unchanged, with applications needing to:

  1. Register to receive push messages with the device
  2. Register with the BlackBerry Push Service in the cloud to allow push messages for the app to begin being delivered
  3. Register with your server side app so it knows there’s a new subscriber
  4. Be ready to receive push messages at any time
  5. Process pushes in a quick and efficient manner

The server-side portion of consumer push is identical between all BlackBerry smartphone versions, meaning you can use your current server-side app and push service credentials.

TITLE_IMAGE

By włodi from London, UK (The Big Red Button Uploaded by Yarl) [CC-BY-SA-2.0 (http://creativecommons.org/licenses/by-sa/2.0)%5D, via Wikimedia Commons

BlackBerry 7

In BlackBerry 7 Java, there were two main ways to set up your application to receive push messages – open a port and begin listening indefinitely or register your port and application details with the device. The former basically required that your application begin a background process when the app was first installed, as well as every time the device started up. When the push arrived on the port opened by the app, the data could then be processed however required by the app, followed by the app once again listening on the open port for the next message. This method also required the app to perform “manual” registration, an HTTP POST, with the BlackBerry Push Service to allow devices to begin receiving push messages for your app.

https://github.com/blackberry/JDE-Samples/tree/master/com/rim/samples/device/httppushdemo (this sample shows Enterprise Push which is similar to the pre-5.0 Push API method through BIS)

The latter approach was introduced in BlackBerry 5 where new APIs were introduced that removed the need to keep an app thread running at all times. Instead, the app would register itself with the device by passing it the assigned port and push app ID, allowing the device to listen for push messages on behalf of the app and launch to pass the push message to be processed. This method provided a simple way to allow the app to perform the registration with the BlackBerry Push Service.

http://www.blackberry.com/developers/docs/7.0.0api/net/rim/blackberry/api/push/package-frame.html

BlackBerry 10

The approach used in BlackBerry 10 is most similar to the last approach mentioned for BlackBerry 7 and is arguably the simplest way to handle push messages yet. One simplification is that developers no longer need to worry about application ports, as the on-device service will now handle port selection directly with the BlackBerry Push Service. Another is that rather than needing to deal with reading network streams to receive the push payload, it is not delivered directly to the app fully available in the data portion of an invocation.

Application flow

The first thing your app will need is an entry in the bar-descriptor.xml file that registers the app to receive invocations from the device push notification service. Below is an example entry. The only thing you need to change is the invoke-target id value to a string unique to your app:

              <invoke-target id="com.unique.string.for.your.app">	
      <type>APPLICATION</type>
      <filter>
         <action>bb.action.PUSH</action>
         <mime-type>application/vnd.push</mime-type>
      </filter>
    </invoke-target>

The second thing to add to the bar-descriptor is the permission for consumer push (this can also be added through the GUI editor in the IDE):

<permission system=”true”>_sys_use_consumer_push</permission>

After the above entries exist, we are ready to start programming. Using the various Push APIs, the following flow needs to occur in the device app:

Create a Session

Frequency: Every time your app launches

This is where your app registers itself to receive push messages. Once your app passes the on-device service, it’s provided a push app ID as well as the invoke-target ID you specified in the bar-descriptor file. This basically tells the service, “If a push comes in with this push app ID, invoke my app using this invoke-target ID”.

PushService* m_pushService; //Member variable creation in header file

//In this case BLACKBERRY_INVOKE_TARGET_ID would be “com.unique.string.for.your.app” from above

m_pushService = new PushService(BLACKBERRY_PUSH_APPLICATION_ID, BLACKBERRY_INVOKE_TARGET_ID);
m_pushService->createSession();

Create a Channel

Frequency: Once for the lifetime of the app

After the session has been established on the device, the app then needs to register itself with the BlackBerry Push Service. This operation is referred to as “creating a channel”. The creation of a channel tells the BlackBerry Push Service, “If a push message arrives with this app ID and is addressed to my device, please let it through”. Without this step, pushes will never be delivered to the device.

m_pushService->createChannel(QUrl(BLACKBERRY_PUSH_URL));

Another important part of this step is that when the channel is created, it should return a “token” which currently is simply the value of the device PIN. This can be sent to your server once retrieved to let it know that a new device has subscribed.

Register to Launch

Frequency: Once for the lifetime of the app

Following the channel creation, your last step is to call the “register to launch” function. Unlike BlackBerry 7, BlackBerry 10 apps do not run all the time. When a push arrives however, the application can be launched into the tiled state to perform processing on the push it’s not already running. In order to be launched when a push arrives, the app must call the “register to launch” function. Otherwise if this function is not called and the application is closed, the push data will be lost.

m_pushService->registerToLaunch();

Once the above steps are completed, your app is ready to start receiving push messages. As push uses invocation, you can handle the messages in about the same way you would for a standard invocation of your app:
https://developer.blackberry.com/cascades/documentation/device_platform/
invocation/receiving_invocation.html

The one difference is that you need to use the PushPayload class to retrieve the push payload from the InvokeRequest’s data:
https://developer.blackberry.com/cascades/documentation/device_comm/push/
cs_receiving_a_push_message.html

Ready to get porting your BlackBerry Java push app over to BlackBerry 10? Here’s a great resource that walks through the entire service including lots of code snippets:
https://developer.blackberry.com/cascades/documentation/device_comm/push/index.html

Need a sample to test your current push credentials? Check our GitHub repositories:
https://github.com/blackberry/Cascades-Samples
https://github.com/blackberry/Cascades-Community-Samples

Never used Push before? Check out this overview of the service:
https://developer.blackberry.com/services/push/?CPID=PUSHAPI00

If you still have questions toss a post in the Push Development forums or send me a tweet:
http://supportforums.blackberry.com/t5/BlackBerry-Push-Development/bd-p/Applications_using_Push_Technology

@garettBeuk

BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 10: NFC and Bluetooth

In part 10 we’re going to cover two wireless connectivity capabilities – NFC and Bluetooth.

TITLE_IMAGE

Bluetooth

Serial Port Profile
Serial Port Profile [SPP] lets you transmit/receive arbitrary data – you can talk to many peripherals, including printers, signature capture pads, or even other phones.

Handling SPP communications is easy on BlackBerry 10. As you’d expect on the POSIX system – it’s done through files. You can read and write to an established SPP connection as you would any other file.

Creating an SPP connection to a server is as hard as:

int bt_spp_open(char *addr, char *service_uuid, bool nonblock);

Creating an SPP server is as hard as:

int bt_spp_open_server(char *service_name, char *service_uuid, bool nonblock, void (*callback)(long param,int fd), long param);

Oooh! Callbacks! C’s “idea” of events… That function is called as soon as a peer connects to you.

However, there is some initialization to take care of. I strongly recommend looking at the demo the Bluetooth development team built here.

Object Sharing
The invocation framework is a huge help here. By sharing, you get the data sent to a Bluetooth peer with very little work.

  InvokeManager * im = new InvokeManager(this);

  InvokeRequest request;
  request.setAction("bb.action.SHARE");
  request.setTarget("sys.btviewer");
  request.setUri("file:///tmp/test.png"); //  not recommended location!
  im->invoke(request);

Bluetooth Low Energy
While BlackBerry Java devices didn’t support Bluetooth Low Energy, BlackBerry 10 does. If you’re working with some LE peripherals or just interested, have a look at this great primer (and demo):

http://supportforums.blackberry.com/t5/Native-Development/BlackBerry-10-Bluetooth-LE-primer-for-developers/ta-p/2287377

NFC

NDEF Tags:

Reading

Reading tags is now handled by the invocation framework. The registration in your bar-descriptor.xml is all you need to have configured – you don’t need any NFC/NDEF specific code to get the payload of a tag. Reading tags is much simpler in BlackBerry 10 than previously.

Let’s break down the invocation target that was used for the Scavenger Hunt application used at some recent BlackBerry Live events:

   
  <invoke-target id="com.rngs.BlackBerryJam.Americas.ScavengerHunt">
     <type>APPLICATION </type>
     <filter>
       <action>bb.action.OPEN </action>
       <mime-type>application/vnd.rim.nfc.ndef </mime-type>
       <property var="uris" 
          value="ndef://2/application/com.rim.devr.2012.bbja" />
     </filter>
   </invoke-target>

invoke-target
	The target id has to be globally unique… 

mime-type
	This is the type we have set internally to specify NDEF tag reading events.

property - value

The value here is where your control comes from. If your URI is specific (like the one above) you’ll only get that content. If your URI is just ndef:// – you’ll get any and all NDEF tags read by the device (assuming the user chooses your application – instead of some other built in one).

The 2 in the URI is the NDEF TNF! In this case, 2=MEDIA. (Some purists may argue that I should’ve used type 4=EXTERNAL – but I didn’t )

The TNF for WELL_KNOWN is 1. The type for Smart Posters is Sp – given this, the URI for accepting Smart Poster tags is:

ndef://1/Sp

Easy!

Want to keep all of your tag reading (and creating) in QML? Then all you need is a JavaScript library to process the received payload from the invocation event, which can be found here.

Writing a NDEF Tag
There are a few ways to write tags. I believe that the quickest is through sharing with the invocation framework.

You can build an NDEF tag duplicator by:

  1. Having an invocation target that receives NDEF tags.
  2. Sharing the NDEF tag payload – to keep on writing tags.

From an aesthetic point of view, sharing interrupts your application when the system card takes control – you can write using low level functionality too.

Other Tech (Peer 2 Peer, SNEP, Card Emulation…)
Want more details – or to see some samples? For NFC on BlackBerry there is only one place to go:

http://supportforums.blackberry.com/t5/Java-Development/NFC-Article-and-Code-Index/ta-p/1538775

BlackBerry Java to BlackBerry 10 Cascades Porting Series – Part 9: Location

In this part of the porting series we’re going to cover Location APIs to acquire the geo position of a device.

Geo Positioning

Integrating geo positioning in your app is a breeze. Speaking at a high level, BlackBerry 10 supports the following positioning sources.

Multiple Fixes

With only 3 lines of code, you can start a location session for periodic updates.

// Create the position source
QGeoPositionInfoSource *source = QGeoPositionInfoSource::createDefaultSource(this); 
// connect the signal to listen for position updates
connect(_positionSource, 
	SIGNAL(positionUpdated(const QGeoPositionInfo &)),
	this, 
	SLOT(positionUpdated(const QGeoPositionInfo &))
)
// Start updates
source->startUpdates();

While this is quick and easy, you can also control other parameters such as the position source or the interval at which location fixes are sent to your slot.

Single Fix

To query for a single location fix, simply change the last line to:

// Request a single update
source->requestUpdate();

Sample App
A comprehensive and open source sample application is available at:

https://github.com/blackberry/Cascades-Samples/tree/master/locationdiagnostics

Documentation
You can also read the developer documentation for Location here:

https://developer.blackberry.com/cascades/documentation/device_platform/location/index.html

API Documentation

https://developer.blackberry.com/cascades/reference/qtmobilitysubset__qgeopositioninfosource.html

The BlackBerry 10 Experience App Framework for Adobe AIR [VIDEO]

Built_for_BlackBerry_10_Logo_Black_RGB

 

I’m pleased to announce the BlackBerry 10 Experience App Framework for AIR. If you’re an AIR developer and you’re looking for an easier way to create applications that follow the BlackBerry 10 UI Guidelines, then this is the framework for you. The BlackBerry 10 Experience App Framework for AIR sets up all of the necessities to make it a lot easier to get up and running. Julian Dolce, our AIR SDK team lead did an excellent job creating samples that are chock-full of comments to help guide you in the right direction. This framework is the perfect way to make it easier for you to achieve Built For BlackBerry status when developing with the AIR SDK for BlackBerry 10.

Read More »

Contribute to BlackBerry Open Source

TITLE_IMAGE

Have you been by our GitHub repositories lately? We’re now hosting 93 repositories, including over 400 samples. Many of these have been created by developers here at BlackBerry, but there have been plenty of contributions from our growing community of Open Source developers. These outstanding people range from developers who have just started developing for BlackBerry, to seasoned veterans, who wrote their first code on BlackBerry OS 4, or even earlier! Besides their impeccable taste in mobile operating systems, they all share an appreciation for open code.

Read More »

Command Line Rocks! Part 2 – Adding Multiple Source Files and Libraries

The previous Command Line Rocks! article focused on creating an app with the smallest possible amount of code. This article will show you how to:

  1. Compile multiple source files into a single executable binary
  2. Leverage the power of external libraries
  3. Create an easy-to-manage project structure

Read More »

BlackBerry Jam Americas 2013 – Interview with Erik Lagerway from Hookflash [VIDEO]

Yesterday, Gurtej had a great post about Hookflash and their port of all major components of both WebRTC and Open Peer libraries to BlackBerry 10. Last week, Luke Reimer from Inside BlackBerry had a chance to interview Erik Lagerway during BlackBerry Jam Americas 2013 – take a look!

Happy Birthday to Dr. Robert Moog! – Celebrating Animoog on BlackBerry 10

Last week at BlackBerry Live in Orlando, Moog Music brought their award-winning synthesizer app, Animoog, to the BlackBerry® 10 platform. Animoog for BlackBerry 10 allows users to create and sculpt music through a variety of sounds, timbres and effects—all in the palm of their hand.

Popular with major musicians, the Animoog app is an evolutionary step and mobile extension of the original electronic analog Moog Synthesizer created by Dr. Robert Moog in the 1960s. Today marks Dr. Robert Moog’s 79th Birthday, and to celebrate both his birthday and his passion for high-tech toolmaking in the service of creativity, we are pleased to announce that the promotional Animoog app price of $0.99 will be extended until the end of the weekend in BlackBerry World before returning back to its original price of $9.99.

We hope that you will join us in celebrating Dr. Robert Moog’s birthday by heading over to BlackBerry World and making some awesome music!

Team BlackBerry is heading down to Africa

Guest post from Marysia- Ed.

TITLE_IMAGE

This June and July our EMEA superstar evangelists will be heading down to Africa to spread the BlackBerry 10 love.

If you have never developed for BlackBerry 10 before and would like to learn how to go from nothing to a fully functioning app using either HTML5 or C++/Qt Cascades, then you should come and join us for one of the tour stops around the continent.

Our team will be on hand all day to give you all the tips and tricks you need to build a beautiful app that users will love and value.

To find out more and to register for a place check out the links below. Spaces are limited and are filling fast so don’t wait!

BlackBerry Jam Tunisia 14th June

BlackBerry Jam Kenya 21st June

BlackBerry Jam Ghana 1st July

BlackBerry Jam Nigeria 3rd July

BlackBerry Jam South Africa date tbc

BlackBerry 10 Dev Alpha – To Brick or Not to Brick… That is the Question…

TITLE_IMAGE

For those developers who’ve been part of the BlackBerry 10 Dev Alpha program with either a BlackBerry 10 Dev Alpha A or BlackBerry 10 Dev Alpha B, there’s a date upon which you may have been keeping a watchful eye: June 30th, 2013… the day the BlackBerry 10 Dev Alpha testing devices are set to be deactivated. Or are they?

The BlackBerry 10 Dev Alpha testing devices were seeded to our development community in advance of the BlackBerry 10 launch to ensure hardware availability to our devs well ahead of launch. Now that launch has come and gone (and so too has the date for the BlackBerry 10 Limited Edition device!), those BlackBerry 10 Dev Alpha testing devices start falling into the category of unsupported hardware since they were never intended to be a full commercial product nor do they support the full OS builds. But really, they still work, so why throw away a perfectly good device? Good news! You don’t have to.

If you saw the blog post advising devs of the BlackBerry 10.1 SDK OS update (SDK OS indicates that it’s not a full OS release, but rather an OS build that features a reduced set of apps – basically what you need to develop and build against), then you’ll be pleased to learn that the 10.1 SDK OS also had the side benefit of removing the date that was to disable the BlackBerry 10 Dev Alpha testing devices. In other words, for any BlackBerry 10 Dev Alpha owners, if you’ve updated to the 10.1 SDK OS build, your device will continue to work beyond the June 30th date, with no imposed expiry date embedded in the OS.

The BlackBerry 10 Dev Alpha A hardware will continue to be officially unsupported and the BlackBerry 10 Dev Alpha B hardware will join those same ranks on June 30th, but if you want to continue to developing apps against those devices beyond that date, you’re welcome to do so.

For more info on the BlackBerry 10 Dev Alpha program and to view other FAQs, please visit the BlackBerry 10 Dev Alpha web page.