»  ANDROID »  Native Android SDK

Native Android SDK

Native android sdk has never been so easy! Our extensive guides for all platforms currently available on the market show how to integrate push notifications within several minutes and with several lines of code. You can choose between native programming tools and a diverse set of cross-platform solutions. We are working hard on extending the list of supported SDKs, plugins and modules, so we advise coming back once in a while to see what's new. If you believe that some great tools should be added here, please let us know, and we will do our best to get them up and running!

Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API. The SDK will run on older devices.
 

NOTE: Push notifications work on Android Emulator. You have to have Google Play account set up on the Emulator.
To integrate BulkPush into your Android application you need to do simple following steps:

1. Get SDK (BulkPush.jar) from the link above.

2. Place it in the libs folder of your Android project.

 

In 99% you don’t have to do this step as adding BulkPush.jar to libs folder adds it to classpath of your project automatically. But if something goes wrong you might need to add it manually:

For Eclipse: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java) For IDEA:http://stackoverflow.com/questions/1051640/correct-way-to-add-lib-jar-to-an-intellij-idea-project.

 

3.a In the main activity add the following receivers:

 

//Registration receiver
    BroadcastReceiver mBroadcastReceiver = new RegisterBroadcastReceiver()
    {
        @Override
        public void onRegisterActionReceive(Context context, Intent intent)
        {
            checkMessage(intent);
        }
    };
 
    //Push message receiver
    private BroadcastReceiver mReceiver = new BasePushMessageReceiver()
    {
        @Override
        protected void onMessageReceive(Intent intent)
        {
       //JSON_DATA_KEY contains JSON payload of push notification.
   System.out.println("intent.getExtras().
   getString(JSON_DATA_KEY)"+intent.getExtras().getString(JSON_DATA_KEY));
             
             
      doOnMessageReceive(intent.getExtras().getString(JSON_DATA_KEY));
        }
    };
 
    //Registration of the receivers
    public void registerReceivers()
    {
        IntentFilter intentFilter = new IntentFilter(getPackageName()
 +".action.PUSH_MESSAGE_RECEIVE");
 
        if (broadcastPush)
            registerReceiver(mReceiver, intentFilter);
 
        registerReceiver(mBroadcastReceiver, new IntentFilter
(getPackageName() + "." 
 +PushManager.REGISTER_BROAD_CAST_ACTION));
    }
 
    public void unregisterReceivers()
    {
        //Unregister receivers on pause
        try
        {
            unregisterReceiver(mReceiver);
        }
        catch (Exception e)
        {
            // pass through
        }
 
        try
        {
            unregisterReceiver(mBroadcastReceiver);
        }
        catch (Exception e)
        {
            //pass through
        }
    }

 

3.b Manage receivers registration in the onPause/onResume functions:

 

 @Override
public void onResume()
{
    super.onResume();
 
    //Re-register receivers on resume
    registerReceivers();
     
}
 
@Override
public void onPause()
{
    super.onPause();
 
    //Unregister receivers on pause
    unregisterReceivers();
     
}

 

3.c In the main activity initialize the PushManager and register receivers:

 

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
         
        //Register receivers for push notifications
         
        registerReceivers();
                 
        final PushManager pushManager = PushManager.getInstance(this);
         
        //Start push manager, this will count app
       //open for bulkpush stats as well
         
        try
        {
            pushManager.onStartup(this);
        }
        catch (Exception e)
        {
  //push notifications are not available
// or AndroidManifest.xml is not configured properly
        }
 
        //Register for push!
        pushManager.registerForPushNotifications();
 
         
        checkMessage(getIntent());
        //other code
    }

    

 

4. In AndroidManifest.xml manifest add the following lines under application tag.
The GCM Project Number is a number, but make sure you prefix it with the letter “A”.
Example:

 




 

Where

BP_APPID BulkPush Application Code
BP_PROJECT_ID Project Number you receive from Google (Locate your Project Number) prefixed with'A'.(seemore:http://developer.android.com/guide/google/gcm/index.html andhttp://developer.android.com/guide/google/gcm/gs.html)
BP_PROJECT_ID value is a number but make sure you prefix it with the letter “A” as in the example above.

5.a In the main activity create the following method:

 

private void checkMessage(Intent intent)
    {
        if (null != intent)
        {
            if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
            {
                doOnMessageReceive(intent.getExtras().getString(
     PushManager.PUSH_RECEIVE_EVENT));
            }
            else if (intent.hasExtra(PushManager.REGISTER_EVENT))
            {
                doOnRegistered(intent.getExtras().getString(
      PushManager.REGISTER_EVENT));
            }
            else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
            {
                doOnUnregisteredError(intent.getExtras().getString(
        PushManager.UNREGISTER_EVENT));
            }
            else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
            {
                doOnRegisteredError(intent.getExtras().getString(
PushManager.REGISTER_ERROR_EVENT));
            }
            else if (intent.hasExtra(
 PushManager.UNREGISTER_ERROR_EVENT))
            {
                doOnUnregistered(intent.getExtras().getString(
  PushManager.UNREGISTER_ERROR_EVENT));
            }
 
            resetIntentValues();
        }
    }
 
/**
     * Will check main Activity intent and if it
contains any bulkpush data, will clear it
     */
 private void resetIntentValues()
    {
        Intent mainAppIntent = getIntent();
 
        if (mainAppIntent.hasExtra(
           PushManager.PUSH_RECEIVE_EVENT))
        {
            mainAppIntent.removeExtra(
           PushManager.PUSH_RECEIVE_EVENT);
        }
        else if (mainAppIntent.hasExtra(
           PushManager.REGISTER_EVENT))
        {
            mainAppIntent.removeExtra(
             PushManager.REGISTER_EVENT);
        }
        else if (mainAppIntent.hasExtra(
              PushManager.UNREGISTER_EVENT))
        {
            mainAppIntent.removeExtra(
            PushManager.UNREGISTER_EVENT);
        }
        else if (mainAppIntent.hasExtra(
                PushManager.REGISTER_ERROR_EVENT))
        {
       mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
        }
        else if (mainAppIntent.hasExtra(PushManager.
                UNREGISTER_ERROR_EVENT))
        {
            mainAppIntent.removeExtra(
              PushManager.UNREGISTER_ERROR_EVENT);
        }
 
        setIntent(mainAppIntent);
    }
    public void doOnRegistered(
          String registrationId)
    {
    Log.e("GCMRegId----->",getString(
        R.string.registered, registrationId));
           //print on logcat
        String str=getString(
          R.string.registered, registrationId);
        ptoken=str;
        mGeneralStatus.setText(str);
    }
 
    public void doOnRegisteredError(String errorId)
    {
        mGeneralStatus.setText(getString(
           R.string.registered_error, errorId));
    }
 
    public void doOnUnregistered(String registrationId)
    {
        mGeneralStatus.setText(getString(
             R.string.unregistered, registrationId));
        ptoken=registrationId;
    }
 
    public void doOnUnregisteredError(String errorId)
    {
        mGeneralStatus.setText(getString(
         R.string.unregistered_error, errorId));
    }
 
    public void doOnMessageReceive(String message)
     
     
    {if( (message!=null)){
         
        Log.e("message1", "message"+message);
        mGeneralStatus.setText(getString(
           R.string.on_message, message));
         Log.e("message2", "message"+message);
 //Parse custom JSON data string.
// You can set background color with custom
//JSON data in the following format: {
//"r" : "10", 
"g" : "200", "b" : "100"}
 // Or open specific screen of the app with
//custom page ID (set ID in the { "id" : "2" } format)]
 AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
         // Set the message to display
         alertbox.setIcon(R.drawable.ic_launcher);
         alertbox.setTitle("BulkPush");
         alertbox.setMessage(message);
// Add a neutral button to the alert box
//and assign a click listener
alertbox.setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
 
           // Click listener on the neutral button of alert box
   public void onClick(
          DialogInterface arg0, int arg1) {
 
                 // The neutral button was clicked
                 Toast.makeText(getApplicationContext(),
"clear notification", Toast.LENGTH_LONG).show();
             
                 
             }}); alertbox.show();
        // AlertDialog
  try
    {
        JSONObject messageJson = new JSONObject(message);
        JSONObject customJson =
            new JSONObject(messageJson.getString("u"));
 
    if (customJson.has("r") &&
        customJson.has("g") &&
           customJson.has("b"))
            {
                int r = customJson.getInt("r");
                int g = customJson.getInt("g");
                int b = customJson.getInt("b");
           View rootView = getWindow().
                           getDecorView().
                           findViewById(
                     android.R.id.content);
        rootView.setBackgroundColor(Color.rgb(r, g, b));
            }
            if (customJson.has("id"))
            {
        Intent intent = new Intent(
          this, SecondActivity.class);
        intent.putExtra(
PushManager.PUSH_RECEIVE_EVENT, messageJson.toString());
                startActivity(intent);
            }
        }
        catch (JSONException e)
        {
            // No custom JSON. Pass this exception
        }}
    }

 

5.b Also call this method from onNewIntent as well.

 

public void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        setIntent(intent);
        //have to check if 
        //we've got new   intent as a part of push notification
        checkMessage(intent);
    }
    

 

5.c In the AndroidManifest.xml make the following changes for this activity. Replace PACKAGE_NAME with the package name for your application.

 












 

6. Add the following changes in your AndroidManifest.xml under manifest tag. Replace PACKAGE_NAME with the package name for your application.
You can find your package name in the AndroidManifest.xml under the manifest tag in the very top of the file.

 








 
   
  
 
  
   
     
    
 
    
    
 
    
    
     
 
    
    
 
    
  

 








 

READ_PHONE_STATE permission: The SDK uses this permission to get DeviceId property of the device if ANDROID_ID is not available. You can omit this permission in your APK. However if ANDROID_ID will not be available, features like AppGroups or cross-application user targeting will not work for this device.


GET_ACCOUNTS permission: GCM requires a Google account if you target Androids lower than 4.0.4

 

7. Add the following changes in your AndroidManifest.xml under application tag. Replace PACKAGE_NAME with the package name for your application.

You can find your package name in the AndroidManifest.xml under the manifest tag in the very top of the file.

 







       
            
            
            
            
            
            
        
 
        
        
        

 

If you need to handle custom notifications from your server, you should create a class extended fromcom.arellomobile.android.push.PushGCMIntentService, and include this custom intent service under the“BP_PUSH_SERVICE” tag in your manifest file.

 

Ex.: In your manifest you have to substitute the.

 



with

 



 

If you would like to use Geo Push Notifications, add the following service to your manifest file:

 





You should also call startTrackingGeoPushes() for your PushManager object. Please see our SDK API here.

That’s it!

For load balancing purposes we may need to change the API endpoints in your application. This happens automatically and handled by our SDK. Please make sure that nothing in your app is blocking access to any URL that contains *.bulkpush.com in it.

Note: If your device is connected to the Internet via WiFi, and messages don’t get through to the device, please make sure GCM ports are not blocked by your firewall.

The device accesses the GCM servers on ports 5228-5230. If your organization has a firewall that restricts the traffic to or from the Internet, you’ll need to configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but it sometimes uses 5229 and 5230. GCM doesn’t provide specific IPs. It changes IPs frequently.

Push Notifications SDK API:

Available here: BulkPush SDK API Documentation

 

 

© 2015 BulkPush.com By Broadnet Technologies Ltd.