Mobile Notifications 2.3.0 released

A new version of Unity Mobile Notifications package has been released.

The most notable new feature is Unified APIs. It allows you to send basic notifications without having to write separate code for Android or iOS.

Full list of changes can be found in the changelog.

1 Like

Can unity 2020.3.x be upgraded to Mobile Notifications 2.3.0? The latest version of Mobile Notifications in PM is 2.2.2, thank you.

2.3.0 officially only support 2021.3+.
I believe it should work fine with 2023.3, I don’t remember breaking changes. Try putting this version manually to the manifest.json file.

Could you perhaps reuse this thread for future release announcements? This way, people can watch the thread and get notified when a new post is made.

This is essentially the same approach taken by several other packages, such as the one found here: https://discussions.unity.com/t/724838

Good, but I can’t set the icons for notifications. The Notification Class has no smallIcon and largeIcon parameters!. How can I change the icons?

You mean the Unified Notification?
https://docs.unity3d.com/Packages/com.unity.mobile.notifications@2.4/api/Unity.Notifications.Notification.html

It only has things that are common to both platforms. Small and large icons are Android specific, AndroidNotification does have them.

Thank you. The package need a sample project implement the icons for android for notifications using the same code for android and ios. It’s confusing and we can’t find any documentation for implement it.

This isn’t possible, because iOS notifications have no icons.

It’s easy. Add in the NotificationCenter.cs runtime two parameters, one for the smallIcon and other for the largeIcon in the ScheduleNotification function. Inside the function, after the Android platform check add the n.SmallIcon and n.LargeIcon and fine! Unity can add this easy to the package and use the icons…

I write the source for the fix if anyone can update the package…


        public static int ScheduleNotification<T>(Notification notification, string category, string smallIcon, string largeIcon, T schedule)
            where T : NotificationSchedule
        {
            CheckInitialized();

#if UNITY_ANDROID
            var n = (AndroidNotification)notification;
            n.SmallIcon = smallIcon;
            n.LargeIcon = largeIcon;
            schedule.Schedule(ref n);
            if (notification.Identifier.HasValue)
            {
                AndroidNotificationCenter.SendNotificationWithExplicitID(n, category, notification.Identifier.Value);
                return notification.Identifier.Value;
            }
            else
                return AndroidNotificationCenter.SendNotification(n, category);
#else
            var n = (iOSNotification)notification;
            if (n == null)
                throw new ArgumentException("Passed notifiation is empty");
            n.CategoryIdentifier = category;
            schedule.Schedule(ref n);
            iOSNotificationCenter.ScheduleNotification(n);
            if (notification.Identifier.HasValue)
                return notification.Identifier.Value;
            // iOSNotification is class and has auto-generated id set at this point
            // for consistency with Android set it back to null, so same Notification can be sent again as new one
            int id = int.Parse(n.Identifier, NumberStyles.None, CultureInfo.InvariantCulture);
            n.Identifier = null;
            return id;
#endif
        }

What about the explicit cast that converts the internal notification to a platform specific notification?

                var androidNotification = (AndroidNotification)notification.InternalNotification;
                androidNotification.SmallIcon = "ic_notification";

We thought the intent was that it should allow us to modify things like SmallIcon. But it seems as this returns a struct instance on Android (on iOS the underlying notification is a class type) so changing anything here has no effect.

The purpose of Unified APIs is to provide not only the API, but also the unified behavior on both platforms. That’s why it doesn’t have API for icons - iOS does not have them.
You can use it to setup common stuff, then cast to AndroidNotification, set icons and send that notification through AndroidNotificationCenter.

I understand that you don’t want to add APIs that are redundant when a platform doesn’t support it. I was more curious about the explicit cast. So if we explicitly cast the platform notification to an AndroidNotification and then make changes on that one to set SmallIcon, then the idea is that you no longer should be able to schedule the notification using the NotificationCenter functions?

Correct, those changes don’t make it to Notification, you have to go platform-specific path.

Gotcha, thanks for the clarification!