Unity and .NET MAUI

Unity games and .NET MAUI apps share one C# client. It registers the device token, keeps the installation between launches, signs the person in and out, reports receipts and reads the inbox. The token itself comes from the platform's push service: APNs on iPhone, Firebase on Android, Push Kit on Huawei phones, ADM on Amazon Fire.

The packages

Unity 1.0.0 and .NET MAUI 1.0.0 pass their tests, and com.bolrach.push and Bolrach.Push.Maui go up on the Unity Package Manager registry and NuGet once the publishing accounts exist.

The client depends on nothing outside the base library. It targets netstandard2.1 for Unity 2022.3 and newer, and net10.0 for MAUI.

Unity

Get the token from the platform, for example with Unity's Mobile Notifications package on iOS and Firebase on Android, and hand it over:

using Bolrach.Push.Unity;

void Start()
{
    BolrachPushUnity.Initialize("YOUR_APP_ID");   // the installation is kept in PlayerPrefs
}

// iOS: the APNs device token as hex
async void OnApnsToken(string hexToken) => await BolrachPushUnity.RegisterApnsAsync(hexToken);

// Android: "fcm" for Google, "hms" for Huawei Push Kit, "adm" for Amazon Fire
async void OnAndroidToken(string token) => await BolrachPushUnity.RegisterAndroidAsync("fcm", token);

async void OnSignedIn(string playerId) => await BolrachPushUnity.LoginAsync(playerId);
async void OnSignedOut() => await BolrachPushUnity.LogoutAsync();

// When a notification arrives or is opened, pass its data: the delivery_id in it is what gets reported.
async void OnOpened(IDictionary<string, string> data) => await BolrachPushUnity.ReportAsync(data, "opened");

Calling a register method again with a new token rotates it on the same installation, so a game that refreshes its token on every launch does not pile up devices.

.NET MAUI

using Bolrach.Push.Maui;

BolrachPushMaui.Initialize("YOUR_APP_ID");   // the installation is kept in MAUI Preferences

// iOS (AppDelegate.RegisteredForRemoteNotifications): the raw token bytes
await BolrachPushMaui.RegisterApnsAsync(deviceToken.ToArray());

// Android (FirebaseMessagingService.OnNewToken)
await BolrachPushMaui.RegisterAndroidAsync("fcm", token);

await BolrachPushMaui.Device.LoginAsync("user_8123");
var inbox = await BolrachPushMaui.Device.ListInboxAsync(limit: 20);

Sending

Your game server sends with POST /messages to the player id you passed to LoginAsync, as on the server page. The C# server SDK is a separate package, Bolrach.Push, listed on the SDKs page.

Machine-readable: openapi.json. Console: bolrach.com/push.