Xamarin Android

**Download Sample

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# ->Adnroid ->Android application;

3) Enter the name, for example, PushwooshSample;

4) Copy 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) Modify PushwooshSample/Properties/AndroidManifest.xml the following way:

7.1) 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:

PW_APPID – Pushwoosh Application ID

PW_PROJECT_ID – Project Number you receive from Google GCM

(see more: http://developer.android.com/guide/google/gcm/index.html and http://developer.android.com/guide/google/gcm/gs.html)

Project ID is a number but make sure you prefix it with the letter **“A”**
Add to manifest tag: (don’t forget that the permission must be called `PACKAGE.permission.C2D_MESSAGE`, where `PACKAGE` is the application’s package name)
**7.2)** Add name to the application name if doesn’t exist yet (like ``)

7.3) Add the following to the application tag:

**8)** Edit your main activity the following way:

8.1) Add two BroadcastReceivers to PushwooshSample namespace:

class LocalMessageBroadcastReceiver : BasePushMessageReceiver { public MainActivity activity {get; set;} protected override void OnMessageReceive (Intent intent) { activity.doOnMessageReceive (intent.GetStringExtra (BasePushMessageReceiver.JsonDataKey)); } } class LocalRegisterBroadcastReceiver : RegisterBroadcastReceiver { public MainActivity activity {get; set;} protected override void OnRegisterActionReceive (Context p0, Intent intent) { activity.checkMessage (intent); } }
**8.2)** Add the following line before `public class MainActivity : Activity`, and don’t forget to change the PACKAGE_NAME with the actual application package name:
[IntentFilter (new string[]{"PACKAGE_NAME.MESSAGE"}, Categories = new string[]{"android.intent.category.DEFAULT"})]
**8.3)** Create fields at MainActivity
LocalMessageBroadcastReceiver mMessageReceiver; LocalRegisterBroadcastReceiver mRegisterReceiver; bool mBroadcastPush = true;
**8.4)** Add to OnCreate method:
mMessageReceiver = new LocalMessageBroadcastReceiver (); mMessageReceiver.activity = this; mRegisterReceiver = new LocalRegisterBroadcastReceiver (); mRegisterReceiver.activity = this; registerReceivers(); ArelloMobile.Push.PushManager manager = ArelloMobile.Push.PushManager.getInstance (this); manager.OnStartup (this); //Register for push! manager.RegisterForPushNotifications(); checkMessage (Intent);
**8.5)** Add methods to MainActivity:
protected override void OnNewIntent(Intent intent) { checkMessage (intent); } public void checkMessage(Intent intent) { if (null != intent) { if (intent.HasExtra(PushManager.PushReceiveEvent)) { doOnMessageReceive(intent.Extras.GetString(PushManager.PushReceiveEvent)); } else if (intent.HasExtra(PushManager.RegisterEvent)) { doOnRegistered(intent.Extras.GetString(PushManager.RegisterEvent)); } else if (intent.HasExtra(PushManager.UnregisterEvent)) { doOnUnregisteredError(intent.Extras.GetString(PushManager.UnregisterEvent)); } else if (intent.HasExtra(PushManager.RegisterErrorEvent)) { doOnRegisteredError(intent.Extras.GetString(PushManager.RegisterErrorEvent)); } else if (intent.HasExtra(PushManager.UnregisterErrorEvent)) { doOnUnregistered(intent.Extras.GetString(PushManager.UnregisterErrorEvent)); } resetIntentValues(); } } public void doOnRegistered(String registrationId) { // code to run if device has succesfully registered } public void doOnRegisteredError(String errorId) { // code to run if device failed to register } public void doOnUnregistered(String registrationId) { // code to run if device has succesfully unregistered } public void doOnUnregisteredError(String errorId) { // code to run if device failed to unregister properly } public void doOnMessageReceive(String message) { // code to run when device receives notification } /** * Will check main Activity intent and if it contains any Pushwoosh data, * will clear it */ private void resetIntentValues() { Intent mainAppIntent = Intent; if (mainAppIntent.HasExtra(PushManager.PushReceiveEvent)) { mainAppIntent.RemoveExtra(PushManager.PushReceiveEvent); } else if (mainAppIntent.HasExtra(PushManager.RegisterEvent)) { mainAppIntent.RemoveExtra(PushManager.RegisterEvent); } else if (mainAppIntent.HasExtra(PushManager.UnregisterEvent)) { mainAppIntent.RemoveExtra(PushManager.UnregisterEvent); } else if (mainAppIntent.HasExtra(PushManager.RegisterErrorEvent)) { mainAppIntent.RemoveExtra(PushManager.RegisterErrorEvent); } else if (mainAppIntent.HasExtra(PushManager.UnregisterErrorEvent)) { mainAppIntent.RemoveExtra(PushManager.UnregisterErrorEvent); } Intent = mainAppIntent; } protected override void OnResume () { base.OnResume (); registerReceivers (); } protected override void OnPause () { base.OnPause (); unregisterReceivers (); } public void registerReceivers() { IntentFilter intentFilter = new IntentFilter(PackageName + ".action.PUSH_MESSAGE_RECEIVE"); if (mBroadcastPush) { RegisterReceiver(mMessageReceiver, intentFilter); } RegisterReceiver(mRegisterReceiver, new IntentFilter(PackageName + "." + PushManager.RegisterBroadCastAction)); } public void unregisterReceivers() { UnregisterReceiver(mMessageReceiver); UnregisterReceiver(mRegisterReceiver); }
**8.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 ArelloMobile.Push;
  • using ArelloMobile.Push.Utils;