Android SDK - manual integration

Use this guide to integrate Pushwoosh Android SDK into your Native Android app

NOTE: Push notifications work on Android Emulator. You must have Google Play account set up on the Emulator.

We recommend Fragments guide:
http://pushwoosh.readme.io/docs/android-sdk-fragments

To integrate Pushwoosh into your Android application fllow these simple steps:

Gradle integration

Replace "+" with the current version of Pushwoosh, Android Support and Google Play Services libraries

compile 'com.pushwoosh:pushwoosh:+'
compile 'com.android.support:support-v4:+'
compile 'com.google.android.gms:play-services-gcm:+'
compile 'com.google.android.gms:play-services-location:+'

Writing code

1. In AndroidManifest.xml manifest add the following lines under application tag:

<meta-data android:name="PW_APPID" android:value="XXXXX-XXXXX" />
<meta-data android:name="PW_PROJECT_ID" android:value="A123456789012" />

Where:
PW_APPID is your Pushwoosh Application Code
PW_PROJECT_ID is the Sender ID you received from Google (Locate your FCM Sender ID), prefixed with A.

2.1. In main activity add the following receivers:

//Registration receiver
BroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver()
{
    @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.
        showMessage("push message is " + intent.getExtras().getString(JSON_DATA_KEY));
    }
};
 
//Registration of receivers
public void registerReceivers()
{
    IntentFilter intentFilter = new IntentFilter(getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
 
    registerReceiver(mReceiver, intentFilter, getPackageName() +".permission.C2D_MESSAGE", null);
     
    registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));        
}
 
public void unregisterReceivers()
{
    //Unregister receivers on pause
    try
    {
        unregisterReceiver(mReceiver);
    }
    catch (Exception e)
    {
        // pass.
    }
     
    try
    {
        unregisterReceiver(mBroadcastReceiver);
    }
    catch (Exception e)
    {
        //pass through
    }
}

2.2. Manage receivers registration in 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();
}

2.3. In main activity initialize PushManager and register receivers:

@Override
  public void onCreate(Bundle savedInstanceState)
  {
      super.onCreate(savedInstanceState);
 
      //Register receivers for push notifications
      registerReceivers();
 
      //Create and start push manager
      PushManager pushManager = PushManager.getInstance(this);
       
      //Start push manager, this will count app open for Pushwoosh 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
  }

3.1. In main activity create the following method:

private void checkMessage(Intent intent)
{
    if (null != intent)
    {
        if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))
        {
            showMessage("push message is " + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));
        }
        else if (intent.hasExtra(PushManager.REGISTER_EVENT))
        {
            showMessage("register");
        }
        else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))
        {
            showMessage("unregister");
        }
        else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))
        {
            showMessage("register error");
        }
        else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))
        {
            showMessage("unregister error");
        }
 
        resetIntentValues();
    }
}
 
/**
 * Will check main Activity intent and clear it of any PushWoosh data
 */
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);
}
 
private void showMessage(String message)
{
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}

3.2. Call this method from onNewIntent as well.

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    setIntent(intent);
 
    checkMessage(intent);
}

That’s it!

Don't block access to *.pushwoosh.com URLs

For load balancing purposes we may need to change the API endpoints in your application. This is handled automatically by our SDK. Make sure that nothing in your app is blocking access to any URL with *.pushwoosh.com in it.

Make sure GCM ports are not blocked

If your device is connected to the Internet via WiFi, and messages don’t get through to the device, 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 traffic to or from the Internet, configure it to allow connectivity with GCM. The ports to open are: 5228, 5229, and 5230. GCM typically only uses 5228, but sometimes 5229 and 5230 as well. GCM doesn’t provide specific IPs, which change frequently.

Android SDK - manual integration

Use this guide to integrate Pushwoosh Android SDK into your Native Android app