{"__v":31,"_id":"5540e67731827a0d007ab219","category":{"__v":15,"_id":"5540e5f131827a0d007ab212","pages":["5540e66b31827a0d007ab217","5540e67731827a0d007ab219","55ac917b5863b817008ae3b4","55acaa4c6b4ff90d00784a92","55acb96418eefd0d0071d553","55acc8aa18eefd0d0071d596","55accd2818eefd0d0071d5a8","55acd06518eefd0d0071d5b6","55acddd7fb7b3c19003739cc","55ace14bfb7b3c19003739d3","55ace3a9fb7b3c19003739d7","55acea24f93f0c0d005b880f","55acf15bf93f0c0d005b8821","55acf3cd18eefd0d0071d5fb","55d1f1c0486de50d00326f17"],"project":"5540ce1b31827a0d007ab1cc","version":"5540ce1c31827a0d007ab1cf","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-04-29T14:08:49.271Z","from_sync":false,"order":4,"slug":"android","title":"Android"},"parentDoc":null,"project":"5540ce1b31827a0d007ab1cc","user":"5540cdbf5cf9682100d61a97","version":{"__v":31,"_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","56bc2f033ee9e70d008b46af","56c2f6efbbf9ec2d00e0fe4f","57595bbb18760817001e8bbe","57d8d9793916800e003dde53"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"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":""},"isReference":false,"order":7,"body":"[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-android-sdk)\n[**Sample Project**](https://github.com/Pushwoosh/pushwoosh-android-sdk/tree/master/Samples/Android-Advanced)\n[**SDK API Docs**](https://github.com/Pushwoosh/pushwoosh-android-sdk/tree/master/Documentation) \n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"**NOTE:** Push notifications work on Android Emulator. You must have Google Play account set up on the Emulator.\\n\\nWe recommend **Fragments guide**: \\nhttp://pushwoosh.readme.io/docs/android-sdk-fragments\"\n}\n[/block]\nTo integrate Pushwoosh into your Android application fllow these simple steps:\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Gradle integration\"\n}\n[/block]\n*Replace \"+\" with the current version of Pushwoosh, Android Support and Google Play Services libraries*\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"compile 'com.pushwoosh:pushwoosh:+'\\ncompile 'com.android.support:support-v4:+'\\ncompile 'com.google.android.gms:play-services-gcm:+'\\ncompile 'com.google.android.gms:play-services-location:+'\",\n      \"language\": \"groovy\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Writing code\"\n}\n[/block]\n**1**. In AndroidManifest.xml manifest add the following lines under application tag:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<meta-data android:name=\\\"PW_APPID\\\" android:value=\\\"XXXXX-XXXXX\\\" />\\n<meta-data android:name=\\\"PW_PROJECT_ID\\\" android:value=\\\"A123456789012\\\" />\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\nWhere:\n*PW_APPID* is your Pushwoosh Application Code\n*PW_PROJECT_ID* is the *Sender ID* you received from Google ([Locate your FCM Sender ID](http://docs.pushwoosh.com/docs/fcm-configuration)), prefixed with A.\n\n**2.1.** In 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 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**2.2.** Manage receivers registration in 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**2.3.** In main activity initialize 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**3.1.** In 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 clear it of any PushWoosh data\\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**3.2.** 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]\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 is handled automatically by our SDK. **Make sure that nothing in your app is blocking access to any URL with *.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, 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 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.\"\n}\n[/block]","excerpt":"Use this guide to integrate Pushwoosh Android SDK into your Native Android app","slug":"native-android-sdk","type":"basic","title":"Android SDK -  manual integration"}

Android SDK - manual integration

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-android-sdk/tree/master/Samples/Android-Advanced) [**SDK API Docs**](https://github.com/Pushwoosh/pushwoosh-android-sdk/tree/master/Documentation) [block:callout] { "type": "info", "body": "**NOTE:** Push notifications work on Android Emulator. You must have Google Play account set up on the Emulator.\n\nWe recommend **Fragments guide**: \nhttp://pushwoosh.readme.io/docs/android-sdk-fragments" } [/block] To integrate Pushwoosh into your Android application fllow these simple steps: [block:api-header] { "type": "basic", "title": "Gradle integration" } [/block] *Replace "+" with the current version of Pushwoosh, Android Support and Google Play Services libraries* [block:code] { "codes": [ { "code": "compile 'com.pushwoosh:pushwoosh:+'\ncompile 'com.android.support:support-v4:+'\ncompile 'com.google.android.gms:play-services-gcm:+'\ncompile 'com.google.android.gms:play-services-location:+'", "language": "groovy" } ] } [/block] [block:api-header] { "type": "basic", "title": "Writing code" } [/block] **1**. In AndroidManifest.xml manifest add the following lines under application tag: [block:code] { "codes": [ { "code": "<meta-data android:name=\"PW_APPID\" android:value=\"XXXXX-XXXXX\" />\n<meta-data android:name=\"PW_PROJECT_ID\" android:value=\"A123456789012\" />", "language": "xml" } ] } [/block] Where: *PW_APPID* is your Pushwoosh Application Code *PW_PROJECT_ID* is the *Sender ID* you received from Google ([Locate your FCM Sender ID](http://docs.pushwoosh.com/docs/fcm-configuration)), prefixed with A. **2.1.** In 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 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] **2.2.** Manage receivers registration in 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] **2.3.** In main activity initialize 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] **3.1.** In 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 clear it of any PushWoosh data\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] **3.2.** 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] 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 is handled automatically by our SDK. **Make sure that nothing in your app is blocking access to any URL with *.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, 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 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." } [/block]