Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by unity_3KoGAU6lZHRhFA, May 12, 2023.

  1. unity_3KoGAU6lZHRhFA

    unity_3KoGAU6lZHRhFA

    Joined:
    Jul 26, 2022
    Posts:
    7
    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.

    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:
    Code (CSharp):
    1. new ToastContentBuilder()
    2.     .AddText("Hello dear Unity Forum community!")
    3.     .Show();
    But unfortunately i haven't managed to get it to work, with the following problems:
    Code (CSharp):
    1. "'ToastContentBuilder' does not contain a definition for 'Show'",
    2. "Unable to resolve reference 'Windows.Foundation.UniversalApiContract'"
    Today, i've found that unity has something built-in. https://docs.unity3d.com/ScriptReference/WSA.Toast.html. 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:
    Code (CSharp):
    1. var toast = UnityEngine.WSA.Toast.Create("", "Hello!");
    2. 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!
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    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.
     
  3. unity_3KoGAU6lZHRhFA

    unity_3KoGAU6lZHRhFA

    Joined:
    Jul 26, 2022
    Posts:
    7
    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.
     
  4. jfresh401

    jfresh401

    Joined:
    Apr 4, 2023
    Posts:
    1
    I am waiting patiently for your reply.
     
    samuelfebrian likes this.
  5. macro9

    macro9

    Joined:
    Oct 6, 2017
    Posts:
    3

    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.