Skip to content

Local push notifications

This page describes examples that request, schedule, and cancel mobile local notifications with Unity Mobile Notifications.

Mobile local notifications are notifications that the app schedules directly on the device without going through the Hive Axyl push server or the FCM or APNs sending servers. They are not included in the scope of the push service Hive Axyl provides, and the app directly manages permissions, scheduling, cancellation, and identifier storage based on Unity Mobile Notifications and the app's policy.

Overall flow

Step Category What you do
1 Unity package, Recipe code Prepare Unity Mobile Notifications and the example code
2 App code, Recipe code Request local notification permission
3 App code, Recipe code Schedule local notifications
4 App code, Recipe code Cancel scheduled notifications
5 App code Restore the APNs forwarder when used with remote push


Unrelated to Hive Axyl server registration

The local notification example does not register device tokens with the Hive Axyl server. The login session, AddPush(), and FCM/APNs sending information are needed only when you implement remote push notifications.

1. Prepare the example code

  • Unity package  Install the Unity Mobile Notifications package in your project.

    Recipe code  Copy the recipe example code into your project.

Install the Unity Mobile Notifications package in your Unity project, and copy the recipe example code.


Item Role
com.unity.mobile.notifications Android/iOS local notification permission requests, scheduling, and cancellation
Notifications/PushPreparationExample.cs Example that restores the APNs forwarder after the remote push preparation call
Notifications/LocalNotification/LocalNotificationExamples.cs Examples of local notification permission requests, scheduling, cancellation, and canceling all


To call the examples from your app code, add the example assembly and the local notification recipe assembly to references in your app's assembly definition. LocalNotificationExamples.asmdef enables the HIVE_AXYL_UNITY_MOBILE_NOTIFICATIONS define when the com.unity.mobile.notifications package is installed.

2. Request permission

  • App code  Implement this in the app.

    Recipe code  Call the recipe code from the app.

To display notifications, first check or request OS notification permission. Because the iOS notification center delegate may switch to the Unity side after the permission request, pass a restore callback if you also use remote APNs push. On Android or on platforms that do not use the APNs forwarder, you can pass an empty callback.

using System;
using System.Threading;
using Hive.Axyl.Samples.Recipes;
using Hive.Axyl.Samples.RecipeExamples;

CancellationToken cancellationToken = default;
ILocalNotificationSource source = CreateLocalNotificationSource();
Action restoreNotificationDelegate = () =>
{
    // Reinstall the APNs delegate forwarder that the app uses.
};

LocalNotificationOutcome outcome =
    await LocalNotificationExamples.RequestPermissionAsync(
        source,
        restoreNotificationDelegate,
        cancellationToken);

switch (outcome.Status)
{
    case LocalNotificationStatus.Success:
        // You can proceed with scheduling local notifications.
        break;

    case LocalNotificationStatus.PermissionDenied:
        // Handle this according to your app's policy, such as guiding the user to the OS settings.
        break;

    default:
        Debug.LogError($"{outcome.FailedStep}: {outcome.Error?.Message}");
        break;
}

A permission denial is not an error but the user's OS permission state. Instead of calling the permission request repeatedly, guide the user to the app settings or the OS settings.

3. Schedule notifications

  • App code  Implement this in the app.

    Recipe code  Call the recipe code from the app.

ScheduleAsync() checks the permission and then schedules a local notification at the specified time. If scheduling succeeds, keep the returned notification ID in the app's storage. You use this ID later to cancel the same notification.

using System;

DateTime fireAt = DateTime.Now.AddHours(8);

LocalNotificationOutcome outcome =
    await LocalNotificationExamples.ScheduleAsync(
        source,
        title: "출석 보상",
        body: "오늘의 보상을 받을 시간입니다.",
        fireAt,
        restoreNotificationDelegate,
        cancellationToken);

if (outcome.Status == LocalNotificationStatus.Success)
{
    int notificationId = outcome.Id;
    // Save notificationId so that you can cancel the same notification later.
}

DateTimeKind.Utc values are treated as UTC, and Local or Unspecified values are treated as local time. In the app, first filter out past times and scheduling conditions that your app's policy does not allow.

4. Cancel scheduled notifications

  • App code  Implement this in the app.

    Recipe code  Call the recipe code from the app.

You can cancel a specific notification with the saved notification ID, or cancel all local notifications the app owns.

LocalNotificationOutcome canceled =
    LocalNotificationExamples.Cancel(source, savedNotificationId);

if (canceled.Status == LocalNotificationStatus.Success)
{
    // Remove savedNotificationId from storage.
}

LocalNotificationOutcome allCanceled =
    LocalNotificationExamples.CancelAll(source);

Call CancelAll() only in a deliberate reset that clears all local notifications the app owns. If cancellation fails, do not delete the saved notification ID; check the error.

5. Use with remote push

  • App code  Implement this in the app.

If you use remote APNs push and Unity Mobile Notifications together, the notification center delegate may switch to the Unity side during the permission request. If the app uses an APNs delegate forwarder, reinstall the forwarder in a finally block whether the permission request or scheduling call ends in success, failure, or cancellation.

Notifications/PushPreparationExample.cs is an example that wraps the PushRecipe.PrepareAsync() call and calls restoreNotificationDelegate() at the end.

IPushTokenSource pushSource = CreatePushTokenSource();

PreparePushOutcome prepared =
    await PushPreparationExample.RunAsync(
        pushSource,
        preparation,
        restoreNotificationDelegate,
        cancellationToken);

For remote push device token registration, see Implement Google Firebase Cloud Messaging or Implement Apple Push Notification Service.


Next steps

To implement Android remote push notifications, see Implement Google Firebase Cloud Messaging. To implement iOS remote push notifications, see Implement Apple Push Notification Service.