Are Mobile Notifications Unified APIs usable?

I’m trying to set mobile notifications for Android and IOS, and was thrilled to see there was this Unified API as part of the Unity package: Unified APIs for Android and iOS | Mobile Notifications | 2.3.2
Has anyone used it? Couldn’t find discussions on the topic.

It turns out it works ok, but it’s missing some basic features like setting icons for Android.
So I guess instead I’ll have to use the platform specific APIs and build my own wrapper. Unless there’s some simpler way of extending the functionality of this Unified API, maybe?

It doesn’t have icons because iOS does not have icons.
I’m interested in usage patter though. Would you use the same icon for all you notifications or few different ones? That is, I would consider adding icon support to the configuration. I don’t think the Notification should have icon support, given that is Android-only thing, it would be rather odd “unification” otherwise.

Yes, my plan is to use the same icons in all Android notifications (similar to how it works in IOS), so it could be part of the initial configuration, like the channel.

iOS shows app icon, as does Android (for small icon) when you don’t specify it.
Anyway, I’ll consider adding configuration options, at least for small icon.

1 Like

But the app icon doesn’t work well as small icon, as it needs to be transparent, which it isn’t necessarily the case.
It would be great if it could be added. Thanks!

Any news on this? Will small icon be added to the configuration? Thank you

2 Likes

I forgot to mention how I solved it, I made a wrapper to call NotificationCenter methods from. It sets specific values for Android so it works similar to IOS:

using System;
using System.Collections;
using Unity.Notifications;
using UnityEngine;
#if UNITY_ANDROID
using Unity.Notifications.Android;
#endif


/// <summary>
/// Provides methods for initializing and scheduling notifications for Android and IOS indistinctly.
/// </summary>
public static class NotificationsWrapper
{
    
    private const string AndroidChannel = "default";
    private const string AndroidChannelName = "Notifications";
    private const string AndroidChannelDescription = "Main notifications";
    private const string AndroidSmallIcon = "app_icon_small";
    private const string AndroidLargeIcon = "app_icon_large";

    public static bool IsInitialized;
    
    /// <summary>
    /// Initializes the notifications system and waits for permission if needed.
    /// </summary>
    /// <returns>An IEnumerator for use in Unity Coroutines.</returns>
    public static IEnumerator InitializeCo()
    {
        if(IsInitialized) yield break;
        
        // Initialization
        var args = NotificationCenterArgs.Default;
        args.AndroidChannelId = AndroidChannel;
        args.AndroidChannelName = AndroidChannelName;
        args.AndroidChannelDescription = AndroidChannelDescription;
        NotificationCenter.Initialize(args);
        
        IsInitialized = true;
        
        // Remove delivered notifications
        NotificationCenter.CancelAllDeliveredNotifications();
        
        // Wait for request permission if needed
        var request = NotificationCenter.RequestPermission();
        if (request.Status == NotificationsPermissionStatus.RequestPending)
            yield return request;
        Debug.Log("[NotificationsManager] Permission result: " + request.Status);
        
        // Place here any code that should run only if notifications permission is granted
    }
    
    /// <summary>
    /// Schedules a notification with the specified title, body, and time.
    /// </summary>
    /// <param name="title">The title of the notification.</param>
    /// <param name="body">The body of the notification.</param>
    /// <param name="time">The time at which the notification should be scheduled.</param>
    public static void ScheduleNotification(string title, string body, DateTime time)
    {
        var schedule = new NotificationDateTimeSchedule(time);
        
        var notification = new Notification()
        {
            Title = title,
            Text = body
        };
        
#if UNITY_ANDROID
        var androidNotification = (AndroidNotification)notification;
        androidNotification.SmallIcon = AndroidSmallIcon;
        androidNotification.LargeIcon = AndroidLargeIcon;
        androidNotification.FireTime = schedule.FireTime;
        AndroidNotificationCenter.SendNotification(androidNotification, AndroidChannel);
#else
        NotificationCenter.ScheduleNotification(notification, schedule);
#endif
    }

    public static void CancelAllScheduledNotifications()
    {
        NotificationCenter.CancelAllScheduledNotifications();
    }
}
2 Likes

I also took this approach, but it’s a shame that the Unified APIs don’t include small icon for Android which is a must, not a nice to have @aurimasc

1 Like

Have you came up with a solution yet? Quite annoying that we can’t use the unified API to schedule Android Notifications with icons.

If AndroidNotification inherited from Notification or a plain interface we would be able to cast properly and pass it to NotificationCenter.ScheduleNotification(…) but right now it’s a struct and the explicit cast doesn’t really help since the method will not take it.

And actually the explicit cast (suggested by comments in code) is just confusing. I was under the assumption (before diving in) that AndroidNotification was actually inherited from Notification and casting would keep my SmallIcon, but since it’s both a struct and not related to Notification it was in vain and caused me some pulling of hair.