map-o file-code-o arrow-directionplatform-webreach-project-type-boarding-passreach-project-type-couponreach-project-type-event-ticketreach-project-type-genericreach-project-type-gift-cardreach-project-type-loyalty-cardreach-project-type-member-card pictures more_vert chain keyboard_arrow_down keyboard_arrow_right

Xamarin

The Urban Airship Xamarin component provides full bindings to the Urban Airship SDK for use in Visual Studio. With this component, Xamarin developers can target both iOS and Android devices with access to the full scope of the Urban Airship SDK, all while remaining in the C#/.NET ecosystem.

We provide native packages for iOS and Android, and two cross-platform packages:

Resources

Setup

Before you begin, set up Push and any other Urban Airship features for iOS and Android. The Xamarin bindings, like the SDKs that they wrap, are platform-specific, so this would be a good time to familiarize yourself with the SDK APIs and features for each platform you wish to target.

Installation

Installing the Urban Airship components is a quick and easy process, seamlessly integrated into Visual Studio. You have two installation options:

If you choose to install the PCL or .NET Standard library, note that it must be installed in each platform-specific project.

All components can be installed via the NuGet package manager.

NuGet

The SDK can be installed via the NuGet package manager:

Core Native Bindings for Android:

PM> Install-Package urbanairship.android.core

Core Native Bindings for iOS:

PM> Install-Package urbanairship.ios

Portable Class Library:

PM> Install-Package urbanairship.portable

.NET Standard Library:

PM> Install-Package urbanairship.netstandard

iOS Integration

Airship Config

Example AirshipConfig.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
     <key>detectProvisioningMode</key>
     <true/>
     <key>developmentAppKey</key>
     <string>Your Development App Key</string>
     <key>developmentAppSecret</key>
     <string>Your Development App Secret</string>
     <key>productionAppKey</key>
     <string>Your Production App Key</string>
     <key>productionAppSecret</key>
     <string>Your Production App Secret</string>
     <key>useWKWebView</key>
     <true/>
  </dict>
</plist>

Provide an AirshipConfig.plist file with the application’s configuration. In order for this file to be visible to the SDK during TakeOff, be sure that its Build Action is set to BundleResource in your app project.

TakeOff

[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
   public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
   {
      UAirship.TakeOff ();
      // Configure airship here
      return true;
   }
}

Note that if the TakeOff process fails due to improper or missing configuration, the shared UAirship instance will be null. The Urban Airship SDK always logs implementation errors at high visibility.

The Urban Airship SDK requires only a single entry point in the app delegate, known as takeOff. Inside your application delegate’s FinishedLaunching method, initialize a shared UAirship instance by calling UAirship takeOff . This will bootstrap the SDK and look for settings specified in the AirshipConfig.plist config file.

Urban Airship Channel IDs

The Channel ID is a unique identifier that ties together an application/device pair on iOS devices. The Channel ID is used to target pushes to specific devices using the Urban Airship API.

Getting the Channel ID:

string channelId = UAirship.Push.ChannelId;

The Channel ID will be logged for all apps. You can get the channelID by adding one extra line to your application:

Don’t worry if this value initially comes back as null on your app’s first run (or after clearing the application data), as the Channel ID will be created and persisted during registration.

Enabling User Notifications

UAirship.Push.UserPushNotificationsEnabled = true;

Enabling UserPushNotificationsEnabled will prompt the user to for permission to send notifications. To increase the likelihood that the user will accept, you should avoid prompting the user for permission immediately, and instead wait for a more appropriate time in the app.

The Urban Airship SDK makes a distinction between “user notifications”, which can be seen by the user, and other forms of push that allow you to send data to your app silently, or in the background. Enabling or disabling user notifications is a preference often best left up to the user, so by default, user notifications are disabled.

Notification Service Extension

In order to take advantage of iOS 10 notification attachments, such as images, animated gifs, and video, you will need to add the Notification Service Extension NuGet package to your project.

Android Integration

Push Provider

The Urban Airship SDK for Android is split into modules which allow you to choose the push providers included in your application. You must install at least one push provider in your Android app. You can install more than one provider, however FCM and GCM modules should not be included in the same app. If both are included, the SDK will log an error message and default to using the FCM module.

Package Name Description
urbanairship.android.core Core SDK support.
urbanairship.android.adm ADM push provider support.
urbanairship.android.fcm FCM push provider support.
urbanairship.android.gcm GCM push provider support.

As with the core SDK bindings, the push provider(s) can be installed via the NuGet package manager:

Airship Config

Example airshipconfig.properties:

developmentAppKey = Your Development App Key
developmentAppSecret = Your Development App Secret

productionAppKey = Your Production App Key
productionAppSecret = Your Production Secret

# Toggles between the development and production app credentials
# Before submitting your application to an app store set to true
inProduction = false

# LogLevel is "VERBOSE", "DEBUG", "INFO", "WARN", "ERROR" or "ASSERT"
developmentLogLevel = DEBUG
productionLogLevel = ERROR

# FCM/GCM Sender ID
gcmSender = Your Google API Project Number

# Notification customization
notificationIcon = ic_notification
notificationAccentColor = #ff0000

Airship config options are a convenient way to pass custom settings to your app without needing to edit the source code. By default the Urban Airship SDK loads these settings from the airshipconfig.properties file located in your application’s Assets directory. Use this file, among other things, to set the backend credentials for your app, and to toggle between development and production builds. In order for this file to be visible to the SDK during TakeOff, be sure that its Build Action is set to AndroidAsset in your app project.

FCM-specific instructions

Example AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.urbanairship.sample">
	<uses-sdk android:minSdkVersion="16" />
  <application android:theme="@style/AppTheme" >
    <!-- START Manual Firebase Additions -->
    <receiver
      android:name="com.google.firebase.iid.FirebaseInstanceIdInternalReceiver"
      android:exported="false" />
    <receiver
      android:name="com.google.firebase.iid.FirebaseInstanceIdReceiver"
      android:exported="true"
      android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="${applicationId}" />
        </intent-filter>
    </receiver>
    <!-- END Manual Firebase Additions -->
  </application>
</manifest>

As shown in Xamarin’s Remote Notifications with Firebase Cloud Messaging document, specify the receiver in the Android Manifest. Then follow FCM Android Setup and FCM Push Provider Setup to configure your application to use FCM.

TakeOff

[Register("com.example.SampleAutopilot")]
public class SampleAutopilot : Autopilot
{

  public override void OnAirshipReady(UAirship airship)
  {
    // perform any post takeOff airship customizations
  }

  public override AirshipConfigOptions CreateAirshipConfigOptions(Context context)
  {
    /* Optionally set your config at runtime
    AirshipConfigOptions options = new AirshipConfigOptions.Builder()
         .SetInProduction(!BuildConfig.DEBUG)
         .SetDevelopmentAppKey("Your Development App Key")
         .SetDevelopmentAppSecret("Your Development App Secret")
         .SetProductionAppKey("Your Production App Key")
         .SetProductionAppSecret("Your Production App Secret")
         .SetNotificationAccentColor(ContextCompat.getColor(this, R.color.color_accent))
         .SetNotificationIcon(R.drawable.ic_notification)
         .Build();
    return options;
    */
    return base.CreateAirshipConfigOptions(context);
  }
}

Assemblyinfo.cs

[assembly: MetaData("com.urbanairship.autopilot", Value = "sample.SampleAutopilot")]

Create a class that extends Autopilot. Register the autopilot in the Assemblyinfo.cs to generate the required metadata in the AndroidManifest.xml file.

Urban Airship Channel IDs

Getting the Channel ID:

string channelId = UAirship.Shared.ChannelId;

The Channel ID is a unique identifier that ties together an application/device pair on a mobile device. The Channel ID is used to target pushes to specific devices using the Urban Airship API. Once a Channel ID is created, it will persist the application until it is either reinstalled, or its internal data is cleared.

Don’t worry if this value initially comes back as null on your app’s first run (or after clearing the application data), as the Channel ID will be created and persisted during registration.

Enabling User Notifications

UAirship.Shared().PushManager.UserNotificationsEnabled = true;

The Urban Airship SDK makes a distinction between “user notifications”, which can be seen by the user, and other forms of push that allow you to send data to your app silently.

Enabling or disabling user notifications is a preference often best left up to the user, so by default, user notifications are disabled. However they are easily enabled with a single call to the PushManager instance.

Native Bindings

Using the native binding libraries is similar to using either the Android or iOS SDKs. Below we provide a simple comparison between setting a named user ID in the native SDK and binding library. In general, the two changes you will notice between the bindings and SDKs are:

Android

Native Java Call

// Set the named user ID
UAirship.shared().getNamedUser().setId("NamedUserID");

Binding Library:

// Set the named user ID
UAirship.Shared().NamedUser.Id = "NamedUserID"

For more information on the Android SDK, please see the Android platform documentation.

iOS

Native Objective-C Call:

// Set the named user ID
[UAirship namedUser].identifier = "NamedUserId"

Binding Library:

// Set the named user ID
UAirship.NamedUser.Identifier = "NamedUserID"

For more information on the iOS SDK, please see the iOS platform documentation.

Portable Class Library and .NET Standard Library

Add Device Tags:

Airship.Instance.EditDeviceTags()
       .AddTag('tag1')
       .AddTag('tag2')
       .RemoveTag('tag3')
       .Apply();

Add Custom Event:

CustomEvent customEvent = new CustomEvent();
customEvent.EventName = "purchased";
customEvent.EventValue = 123.45;
customEvent.TransactionId = "xxxxxxxxx";
customEvent.InteractionId = "your.store/us/en_us/pd/pgid-10978234";
customEvent.InteractionType = "url";
customEvent.AddProperty("category", "shoes");

Airship.Instance.AddCustomEvent(customEvent);

Associate Identifier:

Airship.Instance.AssociateIdentifier("customKey", "customIdentifier");

Display Message Center:

Airship.Instance.DisplayMessageCenter();

Edit Named User Tag Groups:

Airship.Instance.EditNamedUserTagGroups()
       .AddTag("tag1", "group1")
       .AddTag("tag2", "group1")
       .RemoveTag("tag3", "group2")
       .SetTag("pizza", "loyalty")
       .Apply();

Edit Channel Tag Groups:

Airship.Instance.EditChannelTagGroups()
       .AddTag("tag1", "group1")
       .AddTag("tag2", "group1")
       .RemoveTag("tag3", "group2")
       .SetTag("pizza", "loyalty")
       .Apply();

Properties:

// Properties with getters and setters
Airship.Instance.UserNotificationsEnabled = true;
Airship.Instance.LocationEnabled = true;
Airship.Instance.BackgroundLocationEnabled = true;
Airship.Instance.NamedUser = "namedUser123";

// Properties with getters only
List<string> tags = Airship.Instance.Tags;
string channelId = Airship.Instance.ChannelId;
int count = Airship.Instance.MessageCenterCount;
int unreadCount = Airship.Instance.MessageCenterCount;

The portable class library (PCL) and .NET Standard library each provide a unified interface for common SDK calls, allowing users to place common code in a shared location. This is ideal for working with Xamarin Forms – simply choose one of the libraries and place it into both of your platform-specific projects, follow the integration instructions detailed above for each platform, and all of these calls should be available from your shared codebase.

Because the PCL and .NET Standard library currently have no shared interface for initializing the app (i.e., calling TakeOff), you must install the native bindings into each platform-specific project.

All of these calls are accessible through the Airship class, found in the UrbanAirship.Portable and UrbanAirship.NETStandard namespaces. Full documentation for the PCL and .NET Standard libraries can be found here.

Never miss out on product updates

Click "Allow" in the browser prompt in the upper left hand corner of your browser.

Next, we'll allow you to choose what sort of notifications you'd like to receive.

Choose your notification topics

Product Subscription Options

    Platform Subscription Options

      Never miss out on product updates

      Click "Allow" in the browser prompt in the upper left hand corner of your browser.

      Next, we'll allow you to choose what sort of notifications you'd like to receive.

      Choose your notification topics

      Product Subscription Options

        Platform Subscription Options