Sending invocation

An invocation message contains a target, an action, and data. To learn more about these parameters, see Invocation framework. You can configure your app to invoke target apps by using bound or unbound invocations.

Bound invocation

Bound invocation occurs when a client app explicitly identifies the target app to handle the invocation. The following image shows how a client app can use bound invocation to invoke a specific target app. In this case, Target app opens the document contained in the client app.

Diagram showing how a client app can use the bound invocation to invoke a target app of its choice

Before you can send an invocation request, you must add the com.blackberry.invoke plugin to your app. To add the plugin, on the command line, navigate to your project folder and run the following command:

webworks plugin add com.blackberry.invoke

Here's an example that shows how to send an invocation request:

blackberry.invoke.invoke({
target: "com.example.image.view",
action: "bb.action.OPEN",
type: "image/png",
uri: "file:///path/to/image.png"
}, onSuccess, onError);

Once the invocation request is sent, the invocation response is handled by the invocation framework itself. However, you must create JavaScript functions to handle the success or error events. Here's a sample that shows you how to do that:

function onSuccess() {
    console.log("<p>Invocation successful</p>");
}
function onError(error) {
    console.log("<p>Invocation error: " + error + "</p>");
}

Target discovery

You can hard code the target app that you want to send an invocation message to, or you can query the invocation framework to find out what targets are installed on the BlackBerry device. The invocation framework uses a target filter to allow the target apps to declare the types of invocation they support. When a client app queries the invocation framework, the framework uses the target app's filter to describe the kind of target it is and the data it can accept.

The quality of the results depends on how specific the target query request is. A target query request should consist of the URI and MIME type. If the MIME type is omitted, the framework tries to determine the type using the URI.

A client app can include the action to be performed on the data in its target query request. If the action is not specified in the target query request, the query result will contain a list of all the actions that can be performed on the specified data by the available targets.

The invocation framework returns a query result that includes the targets grouped by the action that these targets support. For each target, the query result provides the target ID, the target type (an app or a card), and other information about the target, such as icons and labels, that can be used to display the target in the client app's screen.

To determine what targets are suitable candidates, the invocation framework applies a set of brokering rules. For more information, see Target selection and brokering process.

The following code sample demonstrates a target query request in which a client app searches for targets that support the bb.action.OPEN action on image/png images, which are sent as a file:// URI:

var request = {
    "action": "bb.action.OPEN",
    "type": "image/png",
    "uri": "file://path/to/image.png",
    "target_type": ["APPLICATION", "VIEWER"],
    "action_type": "ALL"
};

blackberry.invoke.query(request, onSuccess, onError);

Here's how you can handle the results:

function onSuccess(response) {
    console.log("<p>Invocation query response: " + response + "</p>");
    var data = JSON.stringify(response);
    //can now parse the data variable for invocation targets
    // that matched the query criteria
}

function onError(error) {
    console.log("<p>Invocation query error: " + error + "</p>");
}

Unbound invocation

The invocation framework can make invoking a target app easy by removing the need to query. Instead of sending a target query request to determine the available targets, the client app can simply send an invocation request to the framework without specifying a target. This kind of invocation is called unbound invocation. If you send an unbound invocation request, the framework searches for and invokes a suitable target app for you.

The following image shows how a client app lets the invocation framework return the most suitable target app for a particular action. In this case, Target app is the most suitable app to open the document contained in the client app.

Diagram showing how a client app lets the invocation framework return the most suitable target app for a particular action.

To find the best target app, the framework first uses a brokering process. It applies a set of rules to determine which target apps support the client app's invocation request. If there is more than one suitable target available, the framework applies a set of selection rules to choose the most appropriate target. For more information, see Target selection and brokering.

You can also send an invocation request without specifying an action. In this type of request, only the data is sent. The framework determines the type of target and the action to be performed.

If you don't specify an action, the invocation framework tries to find an appropriate target app for the bb.action.VIEW action. If no suitable target app is found for bb.action.VIEW, the invocation framework falls back to determine an appropriate target app that supports bb.action.OPEN. If still no suitable target app is available for bb.action.OPEN, the invoke request is unsuccessful.

The following code sample shows how you can create an unbound invocation request. The code invokes the most suitable target app to handle the image.png file, which is specified by the URI.

blackberry.invoke.invoke({
uri: "file:///path/to/image.png"
}, onSuccess, onError);

Last modified: 2014-12-04



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus