{"__v":18,"_id":"55ace3a9fb7b3c19003739d7","category":{"project":"5540ce1b31827a0d007ab1cc","version":"5540ce1c31827a0d007ab1cf","_id":"55b9ff90eb08801900f833e5","pages":[],"__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-07-30T10:42:24.499Z","from_sync":false,"order":21,"slug":"xamarin","title":"Xamarin"},"parentDoc":null,"project":"5540ce1b31827a0d007ab1cc","user":"55a79a4d4a33f92b00b7a111","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":["55e0b30c6bad670d0081f2ea"],"next":{"pages":[],"description":""},"createdAt":"2015-07-20T12:03:53.107Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android/PushwooshSDK)\n[**Download Sample**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android)\n\nTo integrate Pushwoosh into your Xamarin Android application you need to follow these steps:\n\n**1.** Create a new Xamarin solution: **File -> New -> Solution…**;\n\n**2.** Choose the Android application: **C# -> Android -> Android application**;\n\n**3.** Enter a name, for example, **PushwooshSample**;\n\n**4.** Copy [**PushwooshSDK**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android/PushwooshSDK) to PushwooshSample solution directory;\n\n**5.** Right click on **Solution -> Add -> Add Existing Project… -> PushwooshSDK/Pushwoosh.csproj** (the one you copied in step 4);\n\n**6.** Double click on **PushwooshSample/References** and check the following checkboxes: **System, System.Xml, System.Core, Mono.Android, Mono.Android.Support.v4, PushwooshSDK**;\n\n**7.** Add **Xamarin.GooglePlayServices.Gcm** and **Xamarin.GooglePlayServices.Location** components to your project.\n\n**8.** Edit your main activity the following way:\n\n**8.1.** Add two **BroadcastReceivers** to PushwooshSample namespace:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"class LocalMessageBroadcastReceiver : BasePushMessageReceiver\\n{\\n    public MainActivity activity {get; set;}\\n \\n    protected override void OnMessageReceive (Intent intent)\\n    {\\n        activity.doOnMessageReceive (intent.GetStringExtra (BasePushMessageReceiver.JsonDataKey));\\n    }\\n}\\n \\nclass LocalRegisterBroadcastReceiver : BaseRegistrationReceiver\\n{\\n    public MainActivity activity {get; set;}\\n \\n    protected override void OnRegisterActionReceive (Context p0, Intent intent)\\n    {\\n        activity.checkMessage (intent);\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n**8.2.** Add the following line before `public class MainActivity : Activity`, and don’t forget to change  PACKAGE_NAME to the actual application package name:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[IntentFilter (new string[]{\\\"PACKAGE_NAME.MESSAGE\\\"}, Categories = new string[]{\\\"android.intent.category.DEFAULT\\\"})]\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n**8.3.** Create fields at MainActivity:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"LocalMessageBroadcastReceiver mMessageReceiver;\\nLocalRegisterBroadcastReceiver mRegisterReceiver;\\n \\nbool mBroadcastPush = true;\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n**8.4.** Add to OnCreate method:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"mMessageReceiver = new LocalMessageBroadcastReceiver ();\\nmMessageReceiver.activity = this;\\n \\nmRegisterReceiver = new LocalRegisterBroadcastReceiver ();\\nmRegisterReceiver.activity = this;\\nregisterReceivers();\\n \\nPushManager manager = PushManager.GetInstance (this);\\nmanager.OnStartup (this);\\n \\n//Register for push!\\nmanager.RegisterForPushNotifications();\\n \\ncheckMessage (Intent);\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n**8.5.** Add methods to MainActivity:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"protected override void OnNewIntent(Intent intent)\\n   {\\n       checkMessage (intent);\\n   }\\n \\n   public void checkMessage(Intent intent)\\n   {\\n       if (null != intent)\\n       {\\n           if (intent.HasExtra(PushManager.PushReceiveEvent))\\n           {\\n               doOnMessageReceive(intent.Extras.GetString(PushManager.PushReceiveEvent));\\n           }\\n           else if (intent.HasExtra(PushManager.RegisterEvent))\\n           {\\n               doOnRegistered(intent.Extras.GetString(PushManager.RegisterEvent));\\n           }\\n           else if (intent.HasExtra(PushManager.UnregisterEvent))\\n           {\\n               doOnUnregisteredError(intent.Extras.GetString(PushManager.UnregisterEvent));\\n           }\\n           else if (intent.HasExtra(PushManager.RegisterErrorEvent))\\n           {\\n               doOnRegisteredError(intent.Extras.GetString(PushManager.RegisterErrorEvent));\\n           }\\n           else if (intent.HasExtra(PushManager.UnregisterErrorEvent))\\n           {\\n               doOnUnregistered(intent.Extras.GetString(PushManager.UnregisterErrorEvent));\\n           }\\n \\n           resetIntentValues();\\n       }\\n   }\\n \\n   public void doOnRegistered(String registrationId)\\n   {\\n       // run if device has succesfully registered\\n   }\\n \\n   public void doOnRegisteredError(String errorId)\\n   {\\n       // run if device failed to register\\n   }\\n \\n   public void doOnUnregistered(String registrationId)\\n   {\\n       // run if device has succesfully unregistered\\n   }\\n \\n   public void doOnUnregisteredError(String errorId)\\n   {\\n       // run if device failed to unregister properly\\n   }\\n \\n   public void doOnMessageReceive(String message)\\n   {\\n       // run when device receives notification\\n   }\\n \\n   /**\\n* Will check main Activity intent and if it contains any Pushwoosh data,\\n* will clear it\\n*/\\n   private void resetIntentValues()\\n   {\\n       Intent mainAppIntent = Intent;\\n \\n       if (mainAppIntent.HasExtra(PushManager.PushReceiveEvent))\\n       {\\n           mainAppIntent.RemoveExtra(PushManager.PushReceiveEvent);\\n       }\\n       else if (mainAppIntent.HasExtra(PushManager.RegisterEvent))\\n       {\\n           mainAppIntent.RemoveExtra(PushManager.RegisterEvent);\\n       }\\n       else if (mainAppIntent.HasExtra(PushManager.UnregisterEvent))\\n       {\\n           mainAppIntent.RemoveExtra(PushManager.UnregisterEvent);\\n       }\\n       else if (mainAppIntent.HasExtra(PushManager.RegisterErrorEvent))\\n       {\\n           mainAppIntent.RemoveExtra(PushManager.RegisterErrorEvent);\\n       }\\n       else if (mainAppIntent.HasExtra(PushManager.UnregisterErrorEvent))\\n       {\\n           mainAppIntent.RemoveExtra(PushManager.UnregisterErrorEvent);\\n       }\\n \\n       Intent = mainAppIntent;\\n   }\\n \\n   protected override void OnResume ()\\n   {\\n       base.OnResume ();\\n \\n       registerReceivers ();\\n   }\\n \\n   protected override void OnPause ()\\n   {\\n       base.OnPause ();\\n \\n       unregisterReceivers ();\\n   }\\n \\n   public void registerReceivers()\\n   {\\n       IntentFilter intentFilter = new IntentFilter(PackageName + \\\".action.PUSH_MESSAGE_RECEIVE\\\");\\n \\n       if (mBroadcastPush)\\n       {\\n           RegisterReceiver(mMessageReceiver, intentFilter);\\n       }\\n \\n       RegisterReceiver(mRegisterReceiver, new IntentFilter(PackageName + \\\".\\\" + PushManager.RegisterBroadCastAction));\\n   }\\n \\n   public void unregisterReceivers()\\n   {\\n       UnregisterReceiver(mMessageReceiver);\\n       UnregisterReceiver(mRegisterReceiver);\\n   }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n**7.6.** Organize the following imports:\n\n* using System;\n* using Android.App;\n* using Android.Support.V4.App;\n* using Android.Content;\n* using Android.Runtime;\n* using Android.Views;\n* using Android.Widget;\n* using Android.OS;\n* using Org.Json;\n* using Pushwoosh;\n\n**8.** Add the following to **Properties/AndroidManifest.xml**:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<permission android:name=\\\"PACKAGE_NAME.permission.C2D_MESSAGE\\\" android:protectionLevel=\\\"signature\\\"/>\\n<uses-permission android:name=\\\"PACKAGE_NAME.permission.C2D_MESSAGE\\\"/>\\n\\n<application>\\n    <meta-data\\n        android:name=\\\"PW_APPID\\\"\\n        android:value=\\\"ENTER_YOUR_PUSHWOOSH_APPID\\\" />\\n      <meta-data\\n        android:name=\\\"PW_PROJECT_ID\\\"\\n        android:value=\\\"ENTER_YOUR_GCM_PROJECT_NUMBER\\\" />\\n\\n      <receiver\\n        android:name=\\\"com.google.android.gms.gcm.GcmReceiver\\\"\\n        android:exported=\\\"true\\\"\\n        android:permission=\\\"com.google.android.c2dm.permission.SEND\\\" >\\n        <intent-filter>\\n          <action android:name=\\\"com.google.android.c2dm.intent.RECEIVE\\\" />\\n          <category android:name=\\\"PACKAGE_NAME\\\" />\\n        </intent-filter>\\n      </receiver>\\n</application>\",\n      \"language\": \"xml\",\n      \"name\": \"AndroidManifest.xml\"\n    }\n  ]\n}\n[/block]\nSpecify your Pushwoosh Application Code, GCM Project Number and replace `PACKAGE_NAME` with the bundle ID of your project.\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Application icon\",\n  \"body\": \"Do not forget to set **Application icon** in **AndroidManifest.xml**.\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Swiping app from Android Task Switcher\",\n  \"body\": \"When you swipe Xamarin app from Android Task Switcher, for some reason it force closes the app, disabling further pushes to the app until the app is relaunched.\\n\\nThis doesn't happen if you close the app by pressing \\\"Home\\\" or \\\"Back\\\" button.\\n\\nYou can see the following lines in the LogCat:\\n*D/AndroidRuntime( 2947): Shutting down VM*\\n*I/ActivityManager(  874): **Force stopping** com.pushwoosh.xamarin.app appid=10515 user=0: from pid 2947*\"\n}\n[/block]","excerpt":"Integrating Pushwoosh SDK into your Xamarin Android project","slug":"xamarin-android-sdk","type":"basic","title":"Xamarin Android SDK"}

Xamarin Android SDK

Integrating Pushwoosh SDK into your Xamarin Android project

[**Download SDK**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android/PushwooshSDK) [**Download Sample**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android) To integrate Pushwoosh into your Xamarin Android application you need to follow these steps: **1.** Create a new Xamarin solution: **File -> New -> Solution…**; **2.** Choose the Android application: **C# -> Android -> Android application**; **3.** Enter a name, for example, **PushwooshSample**; **4.** Copy [**PushwooshSDK**](https://github.com/Pushwoosh/pushwoosh-xamarin/tree/master/Android/PushwooshSDK) to PushwooshSample solution directory; **5.** Right click on **Solution -> Add -> Add Existing Project… -> PushwooshSDK/Pushwoosh.csproj** (the one you copied in step 4); **6.** Double click on **PushwooshSample/References** and check the following checkboxes: **System, System.Xml, System.Core, Mono.Android, Mono.Android.Support.v4, PushwooshSDK**; **7.** Add **Xamarin.GooglePlayServices.Gcm** and **Xamarin.GooglePlayServices.Location** components to your project. **8.** Edit your main activity the following way: **8.1.** Add two **BroadcastReceivers** to PushwooshSample namespace: [block:code] { "codes": [ { "code": "class LocalMessageBroadcastReceiver : BasePushMessageReceiver\n{\n public MainActivity activity {get; set;}\n \n protected override void OnMessageReceive (Intent intent)\n {\n activity.doOnMessageReceive (intent.GetStringExtra (BasePushMessageReceiver.JsonDataKey));\n }\n}\n \nclass LocalRegisterBroadcastReceiver : BaseRegistrationReceiver\n{\n public MainActivity activity {get; set;}\n \n protected override void OnRegisterActionReceive (Context p0, Intent intent)\n {\n activity.checkMessage (intent);\n }\n}", "language": "csharp" } ] } [/block] **8.2.** Add the following line before `public class MainActivity : Activity`, and don’t forget to change PACKAGE_NAME to the actual application package name: [block:code] { "codes": [ { "code": "[IntentFilter (new string[]{\"PACKAGE_NAME.MESSAGE\"}, Categories = new string[]{\"android.intent.category.DEFAULT\"})]\n", "language": "csharp" } ] } [/block] **8.3.** Create fields at MainActivity: [block:code] { "codes": [ { "code": "LocalMessageBroadcastReceiver mMessageReceiver;\nLocalRegisterBroadcastReceiver mRegisterReceiver;\n \nbool mBroadcastPush = true;", "language": "csharp" } ] } [/block] **8.4.** Add to OnCreate method: [block:code] { "codes": [ { "code": "mMessageReceiver = new LocalMessageBroadcastReceiver ();\nmMessageReceiver.activity = this;\n \nmRegisterReceiver = new LocalRegisterBroadcastReceiver ();\nmRegisterReceiver.activity = this;\nregisterReceivers();\n \nPushManager manager = PushManager.GetInstance (this);\nmanager.OnStartup (this);\n \n//Register for push!\nmanager.RegisterForPushNotifications();\n \ncheckMessage (Intent);", "language": "csharp" } ] } [/block] **8.5.** Add methods to MainActivity: [block:code] { "codes": [ { "code": "protected override void OnNewIntent(Intent intent)\n {\n checkMessage (intent);\n }\n \n public void checkMessage(Intent intent)\n {\n if (null != intent)\n {\n if (intent.HasExtra(PushManager.PushReceiveEvent))\n {\n doOnMessageReceive(intent.Extras.GetString(PushManager.PushReceiveEvent));\n }\n else if (intent.HasExtra(PushManager.RegisterEvent))\n {\n doOnRegistered(intent.Extras.GetString(PushManager.RegisterEvent));\n }\n else if (intent.HasExtra(PushManager.UnregisterEvent))\n {\n doOnUnregisteredError(intent.Extras.GetString(PushManager.UnregisterEvent));\n }\n else if (intent.HasExtra(PushManager.RegisterErrorEvent))\n {\n doOnRegisteredError(intent.Extras.GetString(PushManager.RegisterErrorEvent));\n }\n else if (intent.HasExtra(PushManager.UnregisterErrorEvent))\n {\n doOnUnregistered(intent.Extras.GetString(PushManager.UnregisterErrorEvent));\n }\n \n resetIntentValues();\n }\n }\n \n public void doOnRegistered(String registrationId)\n {\n // run if device has succesfully registered\n }\n \n public void doOnRegisteredError(String errorId)\n {\n // run if device failed to register\n }\n \n public void doOnUnregistered(String registrationId)\n {\n // run if device has succesfully unregistered\n }\n \n public void doOnUnregisteredError(String errorId)\n {\n // run if device failed to unregister properly\n }\n \n public void doOnMessageReceive(String message)\n {\n // run when device receives notification\n }\n \n /**\n* Will check main Activity intent and if it contains any Pushwoosh data,\n* will clear it\n*/\n private void resetIntentValues()\n {\n Intent mainAppIntent = Intent;\n \n if (mainAppIntent.HasExtra(PushManager.PushReceiveEvent))\n {\n mainAppIntent.RemoveExtra(PushManager.PushReceiveEvent);\n }\n else if (mainAppIntent.HasExtra(PushManager.RegisterEvent))\n {\n mainAppIntent.RemoveExtra(PushManager.RegisterEvent);\n }\n else if (mainAppIntent.HasExtra(PushManager.UnregisterEvent))\n {\n mainAppIntent.RemoveExtra(PushManager.UnregisterEvent);\n }\n else if (mainAppIntent.HasExtra(PushManager.RegisterErrorEvent))\n {\n mainAppIntent.RemoveExtra(PushManager.RegisterErrorEvent);\n }\n else if (mainAppIntent.HasExtra(PushManager.UnregisterErrorEvent))\n {\n mainAppIntent.RemoveExtra(PushManager.UnregisterErrorEvent);\n }\n \n Intent = mainAppIntent;\n }\n \n protected override void OnResume ()\n {\n base.OnResume ();\n \n registerReceivers ();\n }\n \n protected override void OnPause ()\n {\n base.OnPause ();\n \n unregisterReceivers ();\n }\n \n public void registerReceivers()\n {\n IntentFilter intentFilter = new IntentFilter(PackageName + \".action.PUSH_MESSAGE_RECEIVE\");\n \n if (mBroadcastPush)\n {\n RegisterReceiver(mMessageReceiver, intentFilter);\n }\n \n RegisterReceiver(mRegisterReceiver, new IntentFilter(PackageName + \".\" + PushManager.RegisterBroadCastAction));\n }\n \n public void unregisterReceivers()\n {\n UnregisterReceiver(mMessageReceiver);\n UnregisterReceiver(mRegisterReceiver);\n }", "language": "csharp" } ] } [/block] **7.6.** Organize the following imports: * using System; * using Android.App; * using Android.Support.V4.App; * using Android.Content; * using Android.Runtime; * using Android.Views; * using Android.Widget; * using Android.OS; * using Org.Json; * using Pushwoosh; **8.** Add the following to **Properties/AndroidManifest.xml**: [block:code] { "codes": [ { "code": "<permission android:name=\"PACKAGE_NAME.permission.C2D_MESSAGE\" android:protectionLevel=\"signature\"/>\n<uses-permission android:name=\"PACKAGE_NAME.permission.C2D_MESSAGE\"/>\n\n<application>\n <meta-data\n android:name=\"PW_APPID\"\n android:value=\"ENTER_YOUR_PUSHWOOSH_APPID\" />\n <meta-data\n android:name=\"PW_PROJECT_ID\"\n android:value=\"ENTER_YOUR_GCM_PROJECT_NUMBER\" />\n\n <receiver\n android:name=\"com.google.android.gms.gcm.GcmReceiver\"\n android:exported=\"true\"\n android:permission=\"com.google.android.c2dm.permission.SEND\" >\n <intent-filter>\n <action android:name=\"com.google.android.c2dm.intent.RECEIVE\" />\n <category android:name=\"PACKAGE_NAME\" />\n </intent-filter>\n </receiver>\n</application>", "language": "xml", "name": "AndroidManifest.xml" } ] } [/block] Specify your Pushwoosh Application Code, GCM Project Number and replace `PACKAGE_NAME` with the bundle ID of your project. [block:callout] { "type": "warning", "title": "Application icon", "body": "Do not forget to set **Application icon** in **AndroidManifest.xml**." } [/block] [block:callout] { "type": "warning", "title": "Swiping app from Android Task Switcher", "body": "When you swipe Xamarin app from Android Task Switcher, for some reason it force closes the app, disabling further pushes to the app until the app is relaunched.\n\nThis doesn't happen if you close the app by pressing \"Home\" or \"Back\" button.\n\nYou can see the following lines in the LogCat:\n*D/AndroidRuntime( 2947): Shutting down VM*\n*I/ActivityManager( 874): **Force stopping** com.pushwoosh.xamarin.app appid=10515 user=0: from pid 2947*" } [/block]