Friday, March 12, 2010

OpenSocial 0.8 - Pushing App. Update







PUSHING AN APPLICATION UPDATE WITH OPENSOCIAL 0.8



Within the OpenSocial 0.8 specification is a definition available for pushing out a notification to the user stream automatically from an application.

What are updates and why should you care?

The update (or status) stream of a user is a listing of what they are currently doing or what they have posted for their friends and connections to see. What’s important to note here is that using this stream can drive quite a bit of traffic to your application if you use it. Personally, I never look through the application gallery of a platform like the Yahoo! Application Platform or MySpace. Most of the time I will only ever add applications when I see an app notification through one of the updates produced by one of my friends.

I mean – sometimes I just need to find out what Disney princess I am right?

In any event, using this update stream is going to push users to your application. This is one of the many tools that we can use to drive more traffic to your application.

How does this work?

Using standard JavaScript, let’s take a look at the steps that we will need to implement to insert an update. I would suggest that this JavaScript be inserted in a function that is driven by a user event, such as a click action.

  1. var params = {}, activity;  
  2. params[opensocial.Activity.Field.TITLE] = "title";  
  3. params[opensocial.Activity.Field.URL] = "http://www.myserver.com/index.php";  
  4. params[opensocial.Activity.Field.BODY] = "body text";  
  5. activity = opensocial.newActivity(params);  
First we need to set up the parameters that will define what will be pushed out in this update. In the above example, we assign a title string, the URL that the title string will be linked to and the body (content) of the update. There are a whole series of options available within an activity request that can be added to the above. These are listed on the OpenSocial documentation here: http://wiki.opensocial.org/index.php?title=Opensocial.Activity_%28v0.8%29#Fields. We then call the newActivity() method to define the activity that we want to build. The parameter that needs to be present in the method is the parameter list that we defined right before that request.

  1. opensocial.requestCreateActivity(  
  2.     activity,  
  3.     opensocial.CreateActivityPriority.LOW,  
  4.     callback);  


We then call requestCreateActivity to actually send the request and push the update out. There are three parameters present in this method:




  • activity – The activity we defined in the first code sample
  • priority – This is the priority that the update should use to send out. This can either be opensocial.CreateActivityPriority.HIGH (or the string ‘HIGH’) or opensocial.CreateActivityPriority.LOW (or the string ‘LOW’). The difference is in whether the user has granted your application permission to push updates out to their stream. If you define a HIGH priority and the user has not granted your application permission to push out updates, the application will attempt to load an authentication flow to allow the user to allow the update out. If you set a LOW priority, if the user has not granted your app permission, the update will be ignored and no authentication flow will be presented.
  • callback – The function that will be executed when the update is inserted

With a few lines of code, you now have an application that can draw in more users using the update stream

No comments:

Post a Comment