Xamarin Android Customization

How to customize Pushwoosh SDK for Xamarin Android project

In App Purchase Tracking

In order to track in-app purchases you should call the SendInappPurchase method of the PushNotificationsManager class when a user buys a product:

public virtual void SendInappPurchase (string sku, BigDecimal price, string currency);
  • sku – the ID of the purchased product

  • price – the price of the product

  • currency – the currency (ex: “USD”)

Example:

PushNotificationsManager.Instance.SendInappPurchase("com.your.app.Marketing", BigDecimal.ValueOf(149.95), "USD");

Deep Linking

Add IntentFilter attribute to your activity class that will handle the deep link as follows:

using Android.Content;
using Android.Content.PM;

[Activity(Label = "PromoActivity", 
              ConfigurationChanges = ConfigChanges.Orientation, 
              ScreenOrientation = ScreenOrientation.Portrait)]
[IntentFilter(new[] { Intent.ActionView }, 
              Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
              DataScheme = "com.pushwoosh", 
              DataHost = "promotion")]
class PromoActivity : Activity

Note that deep link page name (promotion in the example) goes to the host field, notpathPrefix.

In the example above the deep link opens PromoActivity. The basic implementation below displays alert with promo id value for the sake of simplicity.

using System;
using Android.App;
using Android.OS;
using Android.Text;
using Android.Widget;
using Android.Content;
using Android.Content.PM;

namespace PushwooshSample
{
    [Activity(Label = "PromoActivity", 
              ConfigurationChanges = ConfigChanges.Orientation, 
              ScreenOrientation = ScreenOrientation.Portrait)]
    [IntentFilter(new[] { Intent.ActionView }, 
                  Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
                  DataScheme = "com.pushwoosh", 
                  DataHost = "promotion")]
    public class PromoActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Title = "Deep link activity";

            Intent intent = Intent;
            String action = intent.Action;
            var data = intent.Data;
            if (TextUtils.Equals(action, Intent.ActionView))
            {
                OpenUrl(data);
            }
        }

        void OpenUrl(Android.Net.Uri uri)
        {
            String promoId = uri.GetQueryParameter("id");
            Toast.MakeText(ApplicationContext, promoId, ToastLength.Long).Show();
        }
    }
}

Geozone Push Notifications

To implement Geozones, add Pushwoosh.Geozones.Xamarin.Droid nuget package to your project.

To start location tracking, simply call

PushwooshLocation.StartLocationTracking();

Note that AccessFineLocation and AccessCoarseLocation must be enabled in the AndroidManifest.xml for Geozones to work.

Using Badges on Android

Pushwoosh supports setting badge number on the app icon shortcut for the following Android launchers: Sony, Samsung, LG, HTC, Xiaomi, ASUS, ADW, APEX, NOVA, HUAWEI, ZUK, OPPO.

Example:

using Pushwoosh.Badge;

PushwooshBadge.BadgeNumber = badgeInt;

Opening Custom Activity

If you want to start a particular activity in response to push notifications, add the following IntentFilter to that activity:

[Activity]
[IntentFilter(new[] { "${applicationId}.MESSAGE" }, Categories =  new[] { Intent.CategoryDefault })]
public class MyActivity : Activity

Controlling Log Level

In order to assist with debugging and integration, SDK prints all requests to the console by default. When you're ready for the production build, add "PW_LOG_LEVEL" meta-data with value "ERROR" to the AndroidManifest.xml. This way only information about errors will go to the console. Other option could be one of the following:

NONE - No logs from the SDK ERROR - Display only errors in the console WARN - Display warnings and errors INFO - Display informational messages DEBUG - Even debug information is displayed now NOISE - Everything SDK can print

<meta-data android:name="com.pushwoosh.log_level" android:value="ERROR" />

Using ProGuard

Add the following options if you use ProGuard:

-keep class com.pushwoosh.** { *; }
-dontwarn com.pushwoosh.**

Customizing Notification Open Behavior

If you need to select which activity to display as a result of a push notification, you can create custom NotificationServiceExtension and include fully qualified class name of your NotificationServiceExtension in metadata under “com.pushwoosh.notification_service_extension” value.

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Pushwoosh.Notification;

[assembly: MetaData("com.pushwoosh.notification_service_extension", 
           Value = "com.your.package.YourNotificationServiceExtension")]
namespace YourNamespace
{
    [Register("com/your/package/YourNotificationServiceExtension")]
    public class YourNotificationServiceExtension : NotificationServiceExtension
    {
        protected override void StartActivityForPushMessage(PushMessage message)
        {
            // base.StartActivityForPushMessage(message) starts default launcher activity 
            // or activity marked with ${applicationId}.MESSAGE action.
            // Simply do not call it to override this behaviour.

            // start your activity instead:
            Intent launchIntent = new Intent(ApplicationContext, typeof(YourActivity));
            launchIntent.SetFlags(ActivityFlags.NewTask);

            ApplicationContext.StartActivity(launchIntent);
        }
    }
}

Customizing Push Notifications

To customize the view of push notifications you need to create a custom Factory, you can create custom NotificationFactory and include fully qualified class name of your NotificationFactory in metadata under “com.pushwoosh.notification_factory” value.

<meta-data
    android:name="com.pushwoosh.notification_factory"
    android:value="com.your.package.YourNotificationFactory" / >
using System;
using Android.App;
using Android.Runtime;
using Pushwoosh.Notification;

[assembly: MetaData("com.pushwoosh.notification_factory", 
           Value = "com.your.package.YourAppNotificationFactory")]
namespace PushwooshSample
{
    [Register("com/your/package/YourAppNotificationFactory")]
    public class YourAppNotificationFactory : PushwooshNotificationFactory
    {
        public override Notification OnGenerateNotification(PushMessage pushData)
        {
            // TODO: generate and return custom notification

            return base.OnGenerateNotification(pushData);
        }
    }
}

Private Endpoint URL

For Custom Plan subscriptions only. For more details, please contact our Sales team.

Pushwoosh provides Private endpoints for customers with Custom Plan subscriptions. To set up Private endpoint for SDK you need to add the following to your AndroidManifest.xml file:

<meta-data android:name="com.pushwoosh.base_url" android:value="PUSHWOOSH_PRIVATE_ENDPOINT_URL_PROVIDED" />

Share your feedback with us

Your feedback helps us create a better experience, so we would love to hear from you if you have any issues during the SDK integration process. If you face any difficulties, please do not hesitate to share your thoughts with us via this form.

Last updated

#1600: moved in-app statistics to in-apps, added a link to statistics

Change request updated