URL handling on Firefox OS devices works through Web Activities.
To handle web activities with your web app, you need to update your manifest.webapp file for your app. The Grid6 manifest file includes this entry:
"activities": { "view": { "filters": { "type": "url", "regex": "/http:\/\/grid6.us\//" }, "href": "grid.html" } }
We specify a view
activity, and we filter
based on a url
matching the regex http://grid6.us/
, which is the URL of the Grid6 HTTP url. This will later allow us to launch a MozActivity, which, similar to launching an Android Intent, will ask the user what they want to open the URL with. If the Grid6 app is installed, it will allow the option to open the URL in the Grid6 app.
Now that the OS has been notified that our app can handle an activity at http://grid6.us
, we can add some code to our app in the event that it gets launched from the URL.
Since the URL launching relies on Web Activities, we tell our app to handle the activity
message
, and then do something with the activity data.
navigator.mozSetMessageHandler("activity", function(a) { // a.source.data.url contains the URL that the app // was launched with, e.g. http://grid6.us/bbbbbbaa var sourceTail = a.source.data.url.split("/"); // extract "bbbbbbaa" sourceTail = sourceTail[sourceTail.length - 1]; // renders the squares corresponding to the state "bbbbbbaa" renderSquares(sourceTail); });
To launch an app by its URL scheme, create a new MozActivity with the proper URL information. In the Grid6 app, it looks like this:
var intent = new MozActivity({ name: "view", data: { type: "url", url: "http://grid6.us/" + stateToPath() } });
We tell the MozActivity to use the view
action, since that's what we registered in our app manifest file. The data we send includes the Grid6 URL, and the 8-character state string that we will use to render the grid.