Skip to content

Getting Started

Push notifications come in so many flavours these days. The community needed one way of doing things, but the ability to swap providers in and out at will. The reason, push providers, aside from native have been dropping like flies the last several years. The latest to go was AppCenter which it turns out the Xamarin community was heavily invested in.

Native push is the root of all other push providers. It works at the native OS level and is usually feeding other providers like Azure Notification Hubs and Firebase. As such, the setup instructions found within this document generally apply to all.

Setup

General OS setup is mostly the same on all platforms, but please review the specific provider you intend to use from the menu for more information

Shiny.Push
Shiny.Hosting.Maui

Registration

Look to each appropriate provider to see setups for each. The most important function otherwise, is RequestAccess shown below which will give you the push notification token that you can send to your backend.

All providers use the native implementations on the platform to some degree, as such, you will always need to call

using System;
using System.Threading.Tasks;
using Shiny;
using Shiny.Push;

public class PushRegistration
{
    public async Task CheckPermission()
    {
        var push = Host.Current.Services.GetService<IPushManager>();
        var result = await push.RequestAccess();
        if (result.Status == AccessState.Available)
        {
            // good to go

            // you should send this to your server with a userId attached if you want to do custom work
            var value = result.RegistrationToken;
        }
    }
}

Background Delegate

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Shiny.Push;


public class PushDelegate : IPushDelegate
{
    public async Task OnEntry(PushEntryArgs args)
    {
        // fires when the user taps on a push notification
    }

    public async Task OnReceived(IDictionary<string, string> data)
    {
        // fires when a push notification is received (silient or notification)
    }

    public async Task OnTokenChanged(string token)
    {
        // fires when a push notification change is set by the operating system or provider
    }
}

Additional Features

Like other modules in Shiny, there are certain providers that support additional feature sets. Push really only has 1 extra, tagging.

The following providers, support tagging

  • Azure Notification Hubs
  • Firebase

In order to safely support tagging without the need for constantly feature flag or type checking, the following extension methods exist to make life easy

var push = Host.GetService<IPushManager>();

var supported = push.IsTagsSupport();

// tries to set a params list of tags if available
await push.TrySetTags("tag1", "tag2");

// gets a list of currently set tags
var tags = push.TryGetTags();

// requests permission from the user and sets tags if available
var permissionResult = await push.TryRequestAccessWithTags("tag1", "tag2");

Android

No matter what mechanism you use in Push (both iOS & Android), the platform always rules push. For Android, everything is built on Firebase.

To get push entry delegate events to fire, you must add the following an intent filter as shown below to one (likely your main activity) activities

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

namespace YourNamespace;


[Activity(
    Theme = "@style/Maui.SplashTheme",
    MainLauncher = true,
    ConfigurationChanges =
        ConfigChanges.ScreenSize |
        ConfigChanges.Orientation |
        ConfigChanges.UiMode |
        ConfigChanges.ScreenLayout |
        ConfigChanges.SmallestScreenSize |
        ConfigChanges.Density
)]
[IntentFilter(
    new[] {
        Shiny.ShinyPushIntents.NotificationClickAction
    },
    Categories = new[] { 
        "android.intent.category.DEFAULT" 
    }
)]
public class MainActivity : MauiAppCompatActivity
{
}

iOS

iOS is generally pretty straight forward, but does require a few extra steps shown at the top of this page in the library builder.

Customizing iOS Responses

Your push delegate can implement the IApplePushDelegate interface to customize the presentation options and background fetch result. The following is an example of how to do this:

#if IOS
using UIKit;
using UserNotifications;
#endif

public partial class MyPushDelegate : Shiny.Push.IPushDelegate 
{
    // .. left empty for brevity
}

#if IOS
public partial class MyPushDelegate : Shiny.Push.IApplePushDelegate
{
    // this is only used in a foreground call
    public UNNotificationPresentationOptions? GetPresentationOptions(PushNotification notification)
    {
        return UNNotificationPresentationOptions.Alert;
    }

    // this is executed on any content-available push notification
    public UIBackgroundFetchResult? GetFetchResult(PushNotification notification) 
    {
        return UIBackgroundFetchResult.NewData;
    }
}
#endif

## Android

Please ensure to read [Firebase Cloud Messaging Documentation](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages) for how to implement
server side code.

:::note
With all AddPush registrations, we allow you to pass a Firebase Android argument that allows you to create a default channel for your notifications.  You must either
include the CHANNEL with your push payload or set the default channel in your AndroidManifest.xml like so:

```xml
<application>
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="your_channel_id"/>
</application>

:::

Samples