How to send a native windows (toast) notification from unity C# project?

Many modern applications are sending notifications that OS shows to the user so i thought it will be simple.

I’m using the latest stable version of Unity: 2021.3.24f1. The target platform is windows (10 or 11 obviously). Windows has its built-in notification system that any application can be capable of using. How am i gonna create/send/show this notification from unity project(game)?
There are several questions on the internet and unfortunately none of them are answered to this day.

9009196--1241770--12.05.2023.13.38.52 (2).jpg

i’ve made that notification pop up from a .NetFramework console app project created in Visual Studio by just installing a nuget package called Microsoft.Toolkit.Uwp.Notifications. So i thought i could just move the package files into unity, and use the following code to test it:

new ToastContentBuilder()
    .AddText("Hello dear Unity Forum community!")
    .Show();

But unfortunately i haven’t managed to get it to work, with the following problems:

"'ToastContentBuilder' does not contain a definition for 'Show'",
"Unable to resolve reference 'Windows.Foundation.UniversalApiContract'"

Today, i’ve found that unity has something built-in. Unity - Scripting API: Toast. There is no other place on the internet where this feature is mentioned, and with this extremely scarce documentation all i’ve managed to write is:

var toast = UnityEngine.WSA.Toast.Create("", "Hello!");
toast.Show();

it says “for windows store”. i’m not doing anything for windows store, but “maybe” it will work.
According to the documentation, this should show a notification with a simple text “Hello”.
No errors, successfully entering play mode, but nothing is being shown.

what am i doing wrong? and how to solve it? I just want a simple native windows notification message, nothing fancy. I hope this empty unanswered niche will be finally filled. Thank you in advance!

1 Like

UWP applications (Universal Windows Platform, also called WSA in Unity – which is not be confused with Windows Subsystem for Android) are special applications that use different APIs from traditional Windows applications. In Unity, you need to install UWP Build Support module and then UWP will appear as its own platform in the build settings, separate from Windows Standalone. Everything in the UnityEngine.WSA namespace will only work in those special builds and not in the editor.

Unity itself is also using Mono and not .Net (though there is work underway to use modern .Net instead of Mono) and many of the more modern and Windows-specific .Net APIs do not work under Mono. You’ll need to find a .Net library that’s specifically supports Unity or call C++ Windows APIs using a native plugin.

4 Likes

Adrian, THAT is finally the beefy valuable information! Other answers (including docs) i’ve seen on the internet were very confusing. Thank you so much!
I need it to work in the editor and not only in the build. So for now, i think the most straightforward way for me is to call my dll written in C++ because i’ve done that in the past. Knowing that other people are also gonna search for this kind of thing, i will share my specific implementation here as soon as i finish it and see it reliably working.

I am waiting patiently for your reply.

1 Like

Hi Adrian, I am facing the same issue. do you have a chance to make it work? Is someone able to solve this kind of issue? currently, I stopped my development because of this issue since this is one of the main features that I need to develop.

Here I made a PowerShell based implementation, the interesting thing is that PowerShell has the function of directly issuing Toast notification, only you need to provide XML format data, I asked GPT how to write it and changed it based on my needs. It can send a Toast notification when Unity Build is finished. (My English is not good, so Youdao translated my words into English.)

You can check it on my Github:GitHub - HikariXP/Unity-ToastSender: Simple Plugin For Unity Send Window Toast Notification with Non-UWP Build Target.

9775284--1401306--upload_2024-4-17_10-5-6.png

4 Likes

That’s a quite hacky but really creative solution. MS really made it hard to use some of the newer features.

That seems to work perfectly. Thanks for sharing this. Such necro-posts are actually productive and welcome.

Thank you for your contribution! I test it on Unity 6 build and its work great! I will share the detail later, because the file needs to be placed on specific folder.

Edit: The file need to be placed in StreamingAssets folder. to make it works on build and since we use powershell we need to use ExecutionPolicy Bypass to make the call works.