OneSignal Help & Documentation

Welcome to the OneSignal New IA developer hub. You'll find comprehensive guides and documentation to help you start working with OneSignal New IA as quickly as possible, as well as support if you get stuck. Let's jump right in!

Get Started    Support

Xamarin SDK Setup

OneSignal Xamarin SDK Reference. Works with iOS and Android (and derivatives like Amazon).

For Developers

Generate Credentials

Before setting up the Xamarin SDK, you must generate the appropriate credentials for the platform(s) you are releasing on:

iOS - Generate an iOS Push Certificate

Android - Generate a Google Server API Key

Amazon - Generate an Amazon API Key


Setup SDK

1. Getting Started

1.1 Download and unzip the latest OneSignal Xamarin SDK.

1.2 In Xamarin Studio, for your Xamarin application, create a Cross-platform Single View application. Select Use Shared Library for your Shared Code strategy.

1.3 Right click on your solution and go to Add > Add Existing Project. Select OneSignal-Xamarin-SDK/SDK/Common/Com.OneSignal/Com.OneSignal/Com.OneSignal.shproj.

1.4 Follow the steps according to what platform you are targeting:

Android - Xamarin Android Setup

iOS - Xamarin iOS Setup

2. Xamarin.Android Setup

Android

2.1 In the Android project of your Cross-application Xamarin solution, add a link to file OneSignalAndroid.cs located at OneSignal-Xamarin-SDK/SDK/Android/OneSignalAndroid.cs.

2.2 Under references, add the following:

  • The Com.OneSignal shared project
  • The OneSignal.Android.Binding located at OneSignal-Xamarin-SDK-<latest>/OneSignal.Android.Binding/OneSignal.Android.Binding/bin/Release/OneSignal.Android.Binding.dll

2.3 Add the following required components into your application:

  • Android Support Library v4
  • Google Play Services - Location
  • Google Play Services - Cloud Messaging (GCM)
  • Google Play Services - Analytics

2.4 Add the following permissions to AndroidManifest.xml.

<permission android:name="${manifestApplicationId}.permission.C2D_MESSAGE"
            android:protectionLevel="signature" />
<uses-permission android:name="${manifestApplicationId}.permission.C2D_MESSAGE" />

2.5 In your application tag, add the following.

<application ....>

  <receiver android:name="com.onesignal.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
    <intent-filter>
      <action android:name="com.google.android.c2dm.intent.RECEIVE" />
      <category android:name="${manifestApplicationId}" />
    </intent-filter>
  </receiver>
</application>

Make sure to replace all 3 of instances ${manifestApplicationId} with your package name in AndroidManifest.xml.

2.7 Add the following Initialization code to the onCreate method on your MainActivity class of your application.

using Android.App;
using Android.Widget;
using Android.OS;
using Com.OneSignal;

namespace Example.Shared.Application.Droid
{
	[Activity (Label = "Example.Shared.Application", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		int count = 1;

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			// Initialize OneSignal
      OneSignal.StartInit("YOUR_APP_ID", "YOUR_GOOGLE_PROJECT_NUMBER")
        .EndInit();

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button> (Resource.Id.myButton);
			
			button.Click += delegate {
				button.Text = string.Format ("{0} clicks!", count++);
			};
		}
	}
}

2.10 Add the example notification opened and received delegates exampleNotificationReceivedDelegate exampleNotificationOpenedDelegate to run additional code when the notification is either received or opened. (Optional)

using Android.App;
using Android.Widget;
using Android.OS;
using Com.OneSignal;

namespace Example.Shared.Application.Droid
{
	[Activity (Label = "Example.Shared.Application", MainLauncher = true, Icon = "@mipmap/icon")]
	public class MainActivity : Activity
	{
		int count = 1;

		protected override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);
      
      OneSignal.NotificationReceived exampleNotificationReceivedDelegate = delegate (OSNotification notification)
         {
            try
            {
               System.Console.WriteLine("OneSignal Notification Received:\nMessage: {0}", notification.payload.body);
               Dictionary<string, object> additionalData = notification.payload.additionalData;
               
               if (additionalData.Count > 0)
                  System.Console.WriteLine("additionalData: {0}", additionalData);
            }
            catch (System.Exception e)
            {
               System.Console.WriteLine(e.StackTrace);
            }
         };

         // Notification Opened Delegate
         OneSignal.NotificationOpened exampleNotificationOpenedDelegate = delegate (OSNotificationOpenedResult result)
         {
            try
            {
               System.Console.WriteLine("OneSignal Notification opened:\nMessage: {0}", result.notification.payload.body);
               Dictionary<string, object> additionalData = result.notification.payload.additionalData;
               if (additionalData.Count > 0)
                  System.Console.WriteLine("additionalData: {0}", additionalData);
               
               
               List<Dictionary<string, object>> actionButtons = result.notification.payload.actionButtons;
               if (actionButtons.Count > 0)
                  System.Console.WriteLine("actionButtons: {0}", actionButtons);
            }
            catch (System.Exception e)
            {
               System.Console.WriteLine(e.StackTrace);
            }
         };
      
			// Initialize OneSignal
      OneSignal.StartInit("YOUR_APP_ID", "YOUR_GOOGLE_PROJECT_NUMBER")
        .HandleNotificationReceived(exampleNotificationReceivedDelegate)
        .HandleNotificationOpened(exampleNotificationOpenedDelegate)
        .EndInit();

			// Set our view from the "main" layout resource
			SetContentView (Resource.Layout.Main);

			// Get our button from the layout resource,
			// and attach an event to it
			Button button = FindViewById<Button> (Resource.Id.myButton);
			
			button.Click += delegate {
				button.Text = string.Format ("{0} clicks!", count++);
			};
		}
	}
}

3. Xamarin.iOS Setup

3.1 In the iOS project of your Cross-application Xamarin solution, add a link to file OneSignalIOS.cs located at OneSignal-Xamarin-SDK-<latest>/SDK/iOS/OneSignalIOS.cs.

3.2 Under references, add the following:

  • The Com.OneSignal shared project
  • The OneSignal.iOS.Binding located at OneSignal-Xamarin-SDK-<latest>/OneSignal.iOS.Binding/OneSignal.iOS.Binding/bin/Release/OneSignal.iOS.Binding.dll

3.3 In your application's Info.plist, verify your Bundle Identifier matches you App Settings' Bundle ID, Enable Background Modes, allow Remote notifications.

3.4 In AppDelegate.cs, add OneSignal's initialization code in your FinishedLaunching method.

using Foundation;
using UIKit;
using Com.OneSignal;

namespace Example.Shared.Application.iOS
{
	// The UIApplicationDelegate for the application. This class is responsible for launching the
	// User Interface of the application, as well as listening (and optionally responding) to application events from iOS.
	[Register ("AppDelegate")]
	public class AppDelegate : UIApplicationDelegate
	{
		// class-level declarations

		public override UIWindow Window {
			get;
			set;
		}

		public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
		{
			// Initialize OneSignal
         OneSignal.StartInit("YOUR_APP_ID")
           .EndInit();

			return true;
		}
	}
}

3.5 Add the example notification received or opened delegate exampleNotificationReceivedDelegate exampleNotificationOpenedDelegate to run additional code when the notification is opened. (Optional)

using Foundation;
using UIKit;
using Com.OneSignal;

namespace Example.Shared.Application.iOS
{
	// The UIApplicationDelegate for the application. This class is responsible for launching the
	// User Interface of the application, as well as listening (and optionally responding) to application events from iOS.
	[Register ("AppDelegate")]
	public class AppDelegate : UIApplicationDelegate
	{
		// class-level declarations

		public override UIWindow Window {
			get;
			set;
		}

		public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
		{
               OneSignal.NotificationReceived exampleNotificationReceivedDelegate = delegate (OSNotification notification)
         {
            try
            {
               System.Console.WriteLine("OneSignal Notification Received: {0}", notification.payload.body);
               Dictionary<string, object> additionalData = notification.payload.additionalData;

               if (additionalData.Count > 0)
                  System.Console.WriteLine("additionalData: {0}", additionalData);
            }
            catch (System.Exception e)
            {
               System.Console.WriteLine(e.StackTrace);
            }
         };
         
			// Notification Opened Delegate
			OneSignal.NotificationOpened exampleNotificationOpenedDelegate = delegate(OSNotificationOpenedResult result)
         {
				try
				{
               System.Console.WriteLine ("OneSignal Notification opened: {0}", result.notification.payload.body);
               
               Dictionary<string, object> additionalData = result.notification.payload.additionalData;
               List<Dictionary<string, object>> actionButtons = result.notification.payload.actionButtons;
               
               if (additionalData.Count > 0)
                  System.Console.WriteLine("additionalData: {0}", additionalData);

               if (actionButtons.Count > 0)
                  System.Console.WriteLine ("actionButtons: {0}", actionButtons);
				}
				catch (System.Exception e)
				{
					System.Console.WriteLine (e.StackTrace);
				}
			};

			// Initialize OneSignal
      OneSignal.StartInit("YOUR_APP_ID")
        .HandleNotificationReceived(exampleNotificationReceivedDelegate)
        .HandleNotificationOpened(exampleNotificationOpenedDelegate)
        .EndInit();

			return true;
		}
	}
}

Done! You should now be able to send notifications to your Xamarin app.

Xamarin SDK Setup

OneSignal Xamarin SDK Reference. Works with iOS and Android (and derivatives like Amazon).

For Developers