BuzzBox
Android Notifications and Analytics
 
 


Quick Start

This quick start tutorial shows you how to integrate a background task in your Android application using BuzzBox SDK and how to integrate BuzzBox Analytics

Download the sdk

Download our sdk from here and copy it in you project lib folder

Include it in your project build path: in Eclipse: Menu > Project > Build Path > Add Jar

Other resources for this example:

manifest.xml

Add the following permissions in your manifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Add the following Activities and Services

<activity android:name="com.buzzbox.mob.android.scheduler.ui.SchedulerPreferenceActivity" />
<activity android:name="com.buzzbox.mob.android.scheduler.ui.SchedulerLogActivity" />
<activity android:name="com.buzzbox.mob.android.scheduler.ui.NotificationHistoryActivity" />

<receiver android:name="com.buzzbox.mob.android.scheduler.SchedulerReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <action android:name="android.intent.action.TIME_SET"/>
        <action android:name="android.intent.action.DATE_CHANGED"/>
        <action android:name="android.intent.action.TIMEZONE_CHANGED"/>
    </intent-filter>
</receiver>
<service android:name="com.buzzbox.mob.android.scheduler.ScheduledService"></service>

Create a Task

A task is the action you want your App to perform. It must implement the Task interface.
Here it follows an example of a Task that gets the current weather conditions from google api and push it into a Notification.
public class WeatherTask implements Task {

    @Override
    public String getId() {
        return "weather";
    }
    
    @Override
    public String getTitle() {
        return "Current Weather";
    }
    
    @Override
    public TaskResult doWork(ContextWrapper ctx) {
        String zip = "94103";
        
        TaskResult res = new TaskResult();
        try {
            HttpUtilsResponse resp = HttpUtils.http(
                    ctx, 
                    "http://query.yahooapis.com/v1/public/yql?q=
                    select%20item.forecast%20from%20weather.forecast%20where%20
                    location%3D%22"+zip+"%22&format=json", false);
                        
            JSONObject forecastJson = resp.getJSONObjectResponse()
                .getJSONObject("query") .getJSONObject("results") 
                .getJSONArray("channel").getJSONObject(0) 
                .getJSONObject("item") .getJSONObject("forecast");
            
            
            int code = forecastJson.getInt("code");
            String text = forecastJson.getString("text");
            String date = forecastJson.getString("date");
            
            if (code<=21) {
                
                res.addMessage( 
                        new NotificationMessage(null, 
                        "Weather Forecast!", "Bad weather Alert!",
                        date +" : "+ text, R.drawable.scheduler_notification)
                .setNotificationSettings(true, false, false)
                .setNotificationClickIntentClass( null )); // <--  your Activity here
            }
            
        } catch (Exception e) {
            Log.e("weather.task", e.getMessage(), e);
        }
        return res;
    }
}
Notes: your Task can be as simple as creating a NotificationMessage.
BuzzBox SDK also include a Http Client utilily called HttpUtils, that you can use in your own Task.

Set up the Analytics and Run the Scheduler

To simply track installs, daily visits, daily unique visitors and returning users, just add this code to the onCreate method in your main Activity:
int openAppStatus = AnalyticsManager.onOpenApp(this);
if (openAppStatus==AnalyticsManager.OPEN_APP_FIRST_TIME) { // very first time
    SchedulerManager.getInstance()
    	.saveTask(this, "0 8-19 * * 1,2,3,4,5", WeatherTask.class);
    SchedulerManager.getInstance()
    	.restart(this, WeatherTask.class);
    	
} else if (openAppStatus==AnalyticsManager.OPEN_APP_UPGRADE){
    SchedulerManager.getInstance()
    	.restartAll(this, 0); // they need to be rescheduled
}
Your Task will run every hour, from 8am to 7pm, Monday - Friday, according to the cron string 0 8-19 * * 1,2,3,4,5

Show the Configuration UI

Allow your users to change the Notification Settings.
The Settings Activity is included in the sdk and can be configured in the manifest.xml

The following code shows how to open the Settings from a Menu options
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    final int id = item.getItemId();
    if (id == R.id.menu_main_scheduler) {
        SchedulerManager.getInstance()
        	.startConfigurationActivity(YourActivity.this, YourTask.class);
    }
    
    return super.onOptionsItemSelected(item);
}

Scheduler and Notification UI, embedded in the SDK and customizable

The Settings Activity can be configured from the manifest.xml. An example follows:

<meta-data android:name="SchedulerPreferenceActivity.show.daysOfWeek" 
	android:value="true" />          
<meta-data android:name="SchedulerPreferenceActivity.show.frequency" 
	android:value="false" />
<meta-data android:name="SchedulerPreferenceActivity.show.enabledFromTo" 
	android:value="false" />          
<meta-data android:name="SchedulerPreferenceActivity.show.notificationSettings" 
	android:value="true" />
<meta-data android:name="SchedulerPreferenceActivity.show.SelectAtWhatTimes" 
	android:value="true" />

The Settings are saved automatically when the activity is closed.


BuzzBox Inc. 2010-2011