Targeting Interactive Post recipients in Android

In the April 9th, 2013 Google+ Developers Live, Chirag and I demonstrated how to set the recipients for an Interactive Post in Android.  You can watch the video below:

 

Code from the show

I have added a gist to GitHub for the code showing how I targeted Android interactive shares by combining the listPeopleActivity into the ShareActivity. The following snippets cover the relevant code changes relative to the Android quickstart sample:

Adding the PlusClient.OnPeopleLoadedListenter interface, for the asynchronous method from loadPeople:

public class ShareActivity extends FragmentActivity implements View.OnClickListener,
        OnSignedInListener,
        PlusClient.OnPeopleLoadedListener
        {

A few variables for holding the PlusClient, used for getting the Interactive Share intent, and the ArrayList of people, populated in the onPeopleLoaded callback.

    // Add lists for holding the people from people.list
    private ArrayList<Person> mBestRecipients;
    private PlusClient mPlusClient;

The onPeopleLoaded asynchronous callback implementation:

    // The onPeopleLoaded method for the callback handling the response when the visible
    // people are listed
    @Override
    public void onPeopleLoaded(ConnectionResult status, PersonBuffer personBuffer,
            String nextPageToken) {
        if (status.getErrorCode() == ConnectionResult.SUCCESS) {
        	mBestRecipients.clear();
            try {
                int count = personBuffer.getCount();
                for (int i = 0; i < count; i++) {
                	Person toAdd = personBuffer.get(i).freeze();
                	mBestRecipients.add(toAdd);
                }
            } finally {
                personBuffer.close();
            }
        } else {
            Log.e(TAG, "Error when listing people: " + status);
        }

        Intent intent = getInteractivePostIntent(mPlusClient);  
        startActivityForResult(intent, REQUEST_CODE_INTERACTIVE_POST);
    }

The code for adding the recipients to the post:

        	
        if (best){
        	// Use the recipients list created from people.list...best
        	builder.setRecipients(mBestRecipients);
        }

Additional Notes, Errata

There were a few topics that we would have liked to cover in the episode but just didn’t have enough time for, ended up getting glossed over, or might have been lost when we had recorded live.

While I was setting up the APIs console, I mentioned putting in a meaningful name for the project name.  What I should have indicated was to use an appropriate brand name for your company and an appropriate name for this particular client ID.  When you are setting up the project, you want to make sure to get this right because it can be difficult to change it later on.

I really would have liked to show adding specific users by name.  For example, I was missing Chirag from my list of “BEST” people to share with.  If in code, I wanted to make sure that Chirag was always added as a recipient, I would add him by id using the createPerson method from the platform:

        // Create an interactive post builder.
        PlusShare.Builder builder = new PlusShare.Builder(this, plusClient);       

        builder.addCallToAction(LABEL_VIEW_ITEM, callToActionUrl, callToActionDeepLinkId)
        	.setContentUrl(Uri.parse(getString(R.string.plus_example_deep_link_url)))
        	.setContentDeepLinkId(getString(R.string.plus_example_deep_link_id),
                null, null, null)            
            .setText(mEditSendText.getText().toString());     	

        if (best){
        	// Use the recipients list created from people.list...best
<strong>        	mBestRecipients.add(PlusShare.createPerson("118051310819094153327","Chirag Shah"));</strong>
        	builder.setRecipients(mBestRecipients);
        }

In the above code snippet, you can see how I have created the recipient, Chirag Shah in this case, without using the list of people and instead directly adding by ID and display name.  In practice, this is a more common use case and would probably be done using your own database of users.

Speaking of, I also would have liked to have had a longer discussion on who to target posts to.  For example, you could look at the PhotoHunt app that we ship as part of the Google+ platform documentation.  In this sample, we create an internal social graph of users within the app to people they know on Google+ who have installed the app. For that app, we could target interactive posts to the people who the current user has in their Circles on Google+ and who have already installed the app.

Another note on the code I showed on the gist, the best practice for Android would be to pre-load the list of people when the activity loads as opposed to waiting until the user clicks.  That way, the UI doesn’t lag in cases where there is no internet connectivity. Because I wanted to show the before and after code and lazily copied the code from the ListPeopleActivity, I didn’t end up doing this.

notifications

 

 

Screenshot_2013-04-11-11-08-49

A HUGE point that we didn’t cover was how the targeted posts appear across all platforms.  When you specify a specific person, they will get notifications across all of the places that the notifications appear:

  • Search
  • GMail
  • Google+
  • Mobile
  • and so on

As such, these targeted interactive posts are much more effective at reaching the people listed in the post.

Hope you enjoyed the GDL, it was fun to demo! Here’s a snapshot of Chirag and me during the session:

Screen Shot 2013-04-09 at 4.52.11 PM