{"__v":13,"_id":"5540e67731827a0d007ab219","category":{"__v":15,"_id":"5540e5f131827a0d007ab212","project":"5540ce1b31827a0d007ab1cc","version":"5540ce1c31827a0d007ab1cf","pages":["5540e66b31827a0d007ab217","5540e67731827a0d007ab219","55ac917b5863b817008ae3b4","55acaa4c6b4ff90d00784a92","55acb96418eefd0d0071d553","55acc8aa18eefd0d0071d596","55accd2818eefd0d0071d5a8","55acd06518eefd0d0071d5b6","55acddd7fb7b3c19003739cc","55ace14bfb7b3c19003739d3","55ace3a9fb7b3c19003739d7","55acea24f93f0c0d005b880f","55acf15bf93f0c0d005b8821","55acf3cd18eefd0d0071d5fb","55d1f1c0486de50d00326f17"],"reference":false,"createdAt":"2015-04-29T14:08:49.271Z","from_sync":false,"order":4,"slug":"android","title":"Android"},"project":"5540ce1b31827a0d007ab1cc","user":"5540cdbf5cf9682100d61a97","version":{"__v":27,"_id":"5540ce1c31827a0d007ab1cf","project":"5540ce1b31827a0d007ab1cc","createdAt":"2015-04-29T12:27:08.390Z","releaseDate":"2015-04-29T12:27:08.390Z","categories":["5540ce1c31827a0d007ab1d0","5540d91bbb9e762d00f594ad","5540e5f131827a0d007ab212","5540e5febb9e762d00f594d3","5540e61331827a0d007ab213","5540e6195cf9682100d61afa","5540e62631827a0d007ab214","5540e63031827a0d007ab215","5540e63531827a0d007ab216","5540e63e5cf9682100d61afc","5540e6445cf9682100d61afd","5540e64a5cf9682100d61afe","55a4ff5b2e70c0250038050f","55acb28318eefd0d0071d504","55ae1abe8576b92300291c80","55ae453ef302af23000ac109","55af586d555b900d0036d296","55af91dac8a85321007a53c3","55b9fee204775a2f00628071","55b9ff0e04775a2f00628072","55b9ff4604775a2f00628073","55b9ff5fd72d1e1900276a38","55b9ff7f04775a2f00628074","55b9ff90eb08801900f833e5","55b9ffa5d72d1e1900276a39","55b9ffca04775a2f00628075","55b9fffdd72d1e1900276a3a"],"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2015-04-29T14:11:03.930Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"order":1,"body":"[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-android-sdk)\n[**Sample Project**](https://github.com/Pushwoosh/pushwoosh-native-samples/tree/master/Android)\n[**SDK API Docs**](https://github.com/Pushwoosh/pushwoosh-android-sdk/tree/master/Documentation) \n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API. The SDK runs on older devices.\\n\\n**NOTE:** Push notifications work on Android Emulator. You have to have Google Play account set up on the Emulator.\\n\\nWe recommend to use **Fragments guide**: \\nhttp://pushwoosh.readme.io/docs/android-sdk-fragments\"\n}\n[/block]\nTo integrate Pushwoosh into your Android application you need to do simple following steps:\n\n**1.** Get SDK (Pushwoosh.jar) from the link above\n\n**2.** Place it in the **libs** folder of your Android project.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"In 99% you don’t have to do this step as adding Pushwoosh.jar to libs folder adds it to classpath of your project automatically. But if something goes wrong you might need to add it manually:\\n * For Eclipse - http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)\\n * For IDEA - http://stackoverflow.com/questions/1051640/correct-way-to-add-lib-jar-to-an-intellij-idea-project\"\n}\n[/block]\n**3.1.** In the main activity add the following receivers:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"//Registration receiver\\nBroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver()\\n{\\n    :::at:::Override\\n    public void onRegisterActionReceive(Context context, Intent intent)\\n    {\\n        checkMessage(intent);\\n    }\\n};\\n \\n//Push message receiver\\nprivate BroadcastReceiver mReceiver = new BasePushMessageReceiver()\\n{\\n    @Override\\n    protected void onMessageReceive(Intent intent)\\n    {\\n        //JSON_DATA_KEY contains JSON payload of push notification.\\n        showMessage(\\\"push message is \\\" + intent.getExtras().getString(JSON_DATA_KEY));\\n    }\\n};\\n \\n//Registration of the receivers\\npublic void registerReceivers()\\n{\\n    IntentFilter intentFilter = new IntentFilter(getPackageName() + \\\".action.PUSH_MESSAGE_RECEIVE\\\");\\n \\n    registerReceiver(mReceiver, intentFilter, getPackageName() +\\\".permission.C2D_MESSAGE\\\", null);\\n     \\n    registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + \\\".\\\" + PushManager.REGISTER_BROAD_CAST_ACTION));        \\n}\\n \\npublic void unregisterReceivers()\\n{\\n    //Unregister receivers on pause\\n    try\\n    {\\n        unregisterReceiver(mReceiver);\\n    }\\n    catch (Exception e)\\n    {\\n        // pass.\\n    }\\n     \\n    try\\n    {\\n        unregisterReceiver(mBroadcastReceiver);\\n    }\\n    catch (Exception e)\\n    {\\n        //pass through\\n    }\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**3.2.** Manage receivers registration in the onPause/onResume functions\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"@Override\\npublic void onResume()\\n{\\n    super.onResume();\\n     \\n    //Re-register receivers on resume\\n    registerReceivers();\\n}\\n \\n@Override\\npublic void onPause()\\n{\\n    super.onPause();\\n \\n    //Unregister receivers on pause\\n    unregisterReceivers();\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**3.3.** In the main activity initialize the PushManager and register receivers:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"@Override\\n  public void onCreate(Bundle savedInstanceState)\\n  {\\n      super.onCreate(savedInstanceState);\\n \\n      //Register receivers for push notifications\\n      registerReceivers();\\n \\n      //Create and start push manager\\n      PushManager pushManager = PushManager.getInstance(this);\\n       \\n      //Start push manager, this will count app open for Pushwoosh stats as well\\n      try {\\n             pushManager.onStartup(this);\\n      }\\n      catch(Exception e)\\n      {\\n             //push notifications are not available or AndroidManifest.xml is not configured properly\\n      }\\n       \\n      //Register for push!\\n      pushManager.registerForPushNotifications();\\n \\n      checkMessage(getIntent());\\n \\n      // other code\\n  }\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**4.1.** In the main activity create the following method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"private void checkMessage(Intent intent)\\n{\\n    if (null != intent)\\n    {\\n        if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))\\n        {\\n            showMessage(\\\"push message is \\\" + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));\\n        }\\n        else if (intent.hasExtra(PushManager.REGISTER_EVENT))\\n        {\\n            showMessage(\\\"register\\\");\\n        }\\n        else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))\\n        {\\n            showMessage(\\\"unregister\\\");\\n        }\\n        else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))\\n        {\\n            showMessage(\\\"register error\\\");\\n        }\\n        else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))\\n        {\\n            showMessage(\\\"unregister error\\\");\\n        }\\n \\n        resetIntentValues();\\n    }\\n}\\n \\n/**\\n * Will check main Activity intent and if it contains any PushWoosh data, will clear it\\n */\\nprivate void resetIntentValues()\\n{\\n    Intent mainAppIntent = getIntent();\\n \\n    if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))\\n    {\\n        mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);\\n    }\\n    else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))\\n    {\\n        mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);\\n    }\\n    else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))\\n    {\\n        mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);\\n    }\\n    else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))\\n    {\\n        mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);\\n    }\\n    else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))\\n    {\\n        mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);\\n    }\\n \\n    setIntent(mainAppIntent);\\n}\\n \\nprivate void showMessage(String message)\\n{\\n    Toast.makeText(this, message, Toast.LENGTH_LONG).show();\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**4.2.** Also call this method from onNewIntent as well.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"@Override\\nprotected void onNewIntent(Intent intent)\\n{\\n    super.onNewIntent(intent);\\n    setIntent(intent);\\n \\n    checkMessage(intent);\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n**5.** Add the following changes to your AndroidManifest.xml as outlined here: http://docs.pushwoosh.com/docs/androidmanifestxml-modifications\n\n**6.1** Add Google Play Services library as outlined in Android Documentation:\nhttps://developers.google.com/android/guides/setup\n\n**6.2** Add Android Support Library V4 (**android-support-v4.jar**) as outlined here:\nhttps://developer.android.com/tools/support-library/setup.html\n\nThat’s it!\n[block:callout]\n{\n  \"type\": \"danger\",\n  \"title\": \"Don't block access to *.pushwoosh.com URLs\",\n  \"body\": \"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 *.pushwoosh.com in it.**\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Make sure GCM ports are not blocked\",\n  \"body\": \"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.\\nThe 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.\"\n}\n[/block]","excerpt":"Use this guide to integrate Pushwoosh Android SDK into your Native Android app","slug":"native-android-sdk","type":"basic","title":"Native Android SDK"}

Native Android SDK

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

[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-android-sdk) [**Sample Project**](https://github.com/Pushwoosh/pushwoosh-native-samples/tree/master/Android) [**SDK API Docs**](https://github.com/Pushwoosh/pushwoosh-android-sdk/tree/master/Documentation) [block:callout] { "type": "info", "body": "Due to compatibility with Android 4 please make sure you are using at least 11 version of the Android API. The SDK runs on older devices.\n\n**NOTE:** Push notifications work on Android Emulator. You have to have Google Play account set up on the Emulator.\n\nWe recommend to use **Fragments guide**: \nhttp://pushwoosh.readme.io/docs/android-sdk-fragments" } [/block] To integrate Pushwoosh into your Android application you need to do simple following steps: **1.** Get SDK (Pushwoosh.jar) from the link above **2.** Place it in the **libs** folder of your Android project. [block:callout] { "type": "info", "body": "In 99% you don’t have to do this step as adding Pushwoosh.jar to libs folder adds it to classpath of your project automatically. But if something goes wrong you might need to add it manually:\n * For Eclipse - http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-(Java)\n * For IDEA - http://stackoverflow.com/questions/1051640/correct-way-to-add-lib-jar-to-an-intellij-idea-project" } [/block] **3.1.** In the main activity add the following receivers: [block:code] { "codes": [ { "code": "//Registration receiver\nBroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver()\n{\n @Override\n public void onRegisterActionReceive(Context context, Intent intent)\n {\n checkMessage(intent);\n }\n};\n \n//Push message receiver\nprivate BroadcastReceiver mReceiver = new BasePushMessageReceiver()\n{\n @Override\n protected void onMessageReceive(Intent intent)\n {\n //JSON_DATA_KEY contains JSON payload of push notification.\n showMessage(\"push message is \" + intent.getExtras().getString(JSON_DATA_KEY));\n }\n};\n \n//Registration of the receivers\npublic void registerReceivers()\n{\n IntentFilter intentFilter = new IntentFilter(getPackageName() + \".action.PUSH_MESSAGE_RECEIVE\");\n \n registerReceiver(mReceiver, intentFilter, getPackageName() +\".permission.C2D_MESSAGE\", null);\n \n registerReceiver(mBroadcastReceiver, new IntentFilter(getPackageName() + \".\" + PushManager.REGISTER_BROAD_CAST_ACTION)); \n}\n \npublic void unregisterReceivers()\n{\n //Unregister receivers on pause\n try\n {\n unregisterReceiver(mReceiver);\n }\n catch (Exception e)\n {\n // pass.\n }\n \n try\n {\n unregisterReceiver(mBroadcastReceiver);\n }\n catch (Exception e)\n {\n //pass through\n }\n}", "language": "java" } ] } [/block] **3.2.** Manage receivers registration in the onPause/onResume functions [block:code] { "codes": [ { "code": "@Override\npublic void onResume()\n{\n super.onResume();\n \n //Re-register receivers on resume\n registerReceivers();\n}\n \n@Override\npublic void onPause()\n{\n super.onPause();\n \n //Unregister receivers on pause\n unregisterReceivers();\n}", "language": "java" } ] } [/block] **3.3.** In the main activity initialize the PushManager and register receivers: [block:code] { "codes": [ { "code": "@Override\n public void onCreate(Bundle savedInstanceState)\n {\n super.onCreate(savedInstanceState);\n \n //Register receivers for push notifications\n registerReceivers();\n \n //Create and start push manager\n PushManager pushManager = PushManager.getInstance(this);\n \n //Start push manager, this will count app open for Pushwoosh stats as well\n try {\n pushManager.onStartup(this);\n }\n catch(Exception e)\n {\n //push notifications are not available or AndroidManifest.xml is not configured properly\n }\n \n //Register for push!\n pushManager.registerForPushNotifications();\n \n checkMessage(getIntent());\n \n // other code\n }", "language": "java" } ] } [/block] **4.1.** In the main activity create the following method: [block:code] { "codes": [ { "code": "private void checkMessage(Intent intent)\n{\n if (null != intent)\n {\n if (intent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))\n {\n showMessage(\"push message is \" + intent.getExtras().getString(PushManager.PUSH_RECEIVE_EVENT));\n }\n else if (intent.hasExtra(PushManager.REGISTER_EVENT))\n {\n showMessage(\"register\");\n }\n else if (intent.hasExtra(PushManager.UNREGISTER_EVENT))\n {\n showMessage(\"unregister\");\n }\n else if (intent.hasExtra(PushManager.REGISTER_ERROR_EVENT))\n {\n showMessage(\"register error\");\n }\n else if (intent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))\n {\n showMessage(\"unregister error\");\n }\n \n resetIntentValues();\n }\n}\n \n/**\n * Will check main Activity intent and if it contains any PushWoosh data, will clear it\n */\nprivate void resetIntentValues()\n{\n Intent mainAppIntent = getIntent();\n \n if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT))\n {\n mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);\n }\n else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT))\n {\n mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);\n }\n else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT))\n {\n mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);\n }\n else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT))\n {\n mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);\n }\n else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT))\n {\n mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);\n }\n \n setIntent(mainAppIntent);\n}\n \nprivate void showMessage(String message)\n{\n Toast.makeText(this, message, Toast.LENGTH_LONG).show();\n}", "language": "java" } ] } [/block] **4.2.** Also call this method from onNewIntent as well. [block:code] { "codes": [ { "code": "@Override\nprotected void onNewIntent(Intent intent)\n{\n super.onNewIntent(intent);\n setIntent(intent);\n \n checkMessage(intent);\n}", "language": "java" } ] } [/block] **5.** Add the following changes to your AndroidManifest.xml as outlined here: http://docs.pushwoosh.com/docs/androidmanifestxml-modifications **6.1** Add Google Play Services library as outlined in Android Documentation: https://developers.google.com/android/guides/setup **6.2** Add Android Support Library V4 (**android-support-v4.jar**) as outlined here: https://developer.android.com/tools/support-library/setup.html That’s it! [block:callout] { "type": "danger", "title": "Don't block access to *.pushwoosh.com URLs", "body": "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 *.pushwoosh.com in it.**" } [/block] [block:callout] { "type": "info", "title": "Make sure GCM ports are not blocked", "body": "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.\nThe 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." } [/block]