Android SDK integration


Step 1: Import the Pushbots library into your project:


A. Open build.gradle (Module: app) file:
A.1. Add PushBots dependency along with Play services:

compile 'com.google.android.gms:play-services-gcm:+'
compile 'com.pushbots:pushbots-lib:3.0.1@aar'

A.2. Add to defaultConfig section, then replace PUSHBOTS_APP_ID and GOOGLE_SENDER_ID:

manifestPlaceholders = [manifestApplicationId         : "${applicationId}",
                        pushbots_app_id               : "PUSHBOTS_APP_ID",
                        pushbots_loglevel             : "DEBUG", 
                        google_sender_id              : "GOOGLE_SNEDER_ID"]

B. Sync Project with gradle files:



Step 2: Add required code:


Create a Class that extends Application class (MyApplication.java), and initialize Pushbots instance in it, copy the following just below [package ….;] line:

import android.app.Application;
import com.pushbots.push.Pushbots;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Initialize Pushbots Library
        Pushbots.sharedInstance().init(this);
    }
}

A. Add to MyApplication class to application tag:

android:name=".MyApplication"

in your MainActivity add this line to register for notifications:

//Register for Push Notifications
Pushbots.sharedInstance().registerForRemoteNotifications();

That's pretty much it. Run your application now, if all goes well, your device should be registered and ready to recieve push notifications.


Step 3: Custom handler for notifications (Optional):


3.1. Create customHandler class.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.pushbots.push.Pushbots;
import com.pushbots.push.utils.PBConstants;


public class customHandler extends BroadcastReceiver
{
    private String TAG = "PB3";

    @Override
    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        Log.d(TAG, "action=" + action);

        // Handle Push Message when opened
        if (action.equals(PBConstants.EVENT_MSG_OPEN)) {

            //Bundle containing all fields of the opened notification
            Bundle bundle = intent.getExtras().getBundle(PBConstants.EVENT_MSG_OPEN);

            //Record opened notification
            Pushbots.PushNotificationOpened(context, bundle);

            Log.i(TAG, "User clicked notification with Message: " + bundle.get("message"));

            //Get Custom field key e.g. article_id
            if(bundle.get("article_id") != null)
                Log.i(TAG, "Article Id: " + bundle.get("article_id"));

            //Start Launch Activity
            String packageName = context.getPackageName();
            Intent resultIntent = new Intent(context.getPackageManager().getLaunchIntentForPackage(packageName));
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);

            // Check for next activity
            String next_activity = bundle.getString("nextActivity");
            if(null != next_activity){
                try {
                    Log.i(TAG, "Opening Custom Activity " + next_activity);
                    resultIntent = new Intent(context, Class.forName(next_activity));
                    resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                } catch (ClassNotFoundException e) {
                    //ClassNotFound
                    e.printStackTrace();
                }
            }

            // Check for open URL
            String open_url = bundle.getString("openURL");
            if( null != open_url && ( open_url.startsWith("http://") || open_url.startsWith("https://")) ){
                resultIntent = new Intent("android.intent.action.VIEW", Uri.parse(open_url));
                resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);
                Log.d(TAG, "Opening url: " + open_url);
            }

            resultIntent.putExtras(intent.getBundleExtra("pushData"));

            //Open activity or URL with pushData.
            if(null != resultIntent) {
                context.startActivity(resultIntent);
            }

        }else if(action.equals(PBConstants.EVENT_MSG_RECEIVE)){

            //Bundle containing all fields of the notification
            Bundle bundle = intent.getExtras().getBundle(PBConstants.EVENT_MSG_RECEIVE);
            Log.i(TAG, "User received notification with Message: " + bundle.get("message"));

        }

    }
}
3.2. Open AndroidManifest.xml and add custom Handler receiver in application tag:
<receiver android:name=".customHandler" />
3.3. Set your customHandler class in MyApplication or ManiActivity:
Pushbots.sharedInstance().setCustomHandler(customHandler.class);