Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows local notification using Microsoft.Toolkit.Uwp.Notifications #24788

Open
true-perfect-code opened this issue Sep 16, 2024 · 1 comment
Labels
platform/windows 🪟 t/bug Something isn't working
Milestone

Comments

@true-perfect-code
Copy link

Description

Hello MAUI development team and contributors,

I am trying to implement a local notification in MAUI on the Windows platform. For this I have used the description of gurrenm3 on the Github (https://github.com/gurrenm3/Toast-Notifications-with-.Net-Maui?tab=readme-ov-file ). This works well with sending a notification, but it doesn't work when the Windows app is opened via the notification, because then I can't get to the argument.
I could also use the notification this way, but it is really stupid for the user if he has to search for his entry in the open app himself. My idea is to pass a parameter (argument) when creating the notification, which I do with

                new ToastContentBuilder()
                    .AddArgument("action", "viewConversation")
                    .AddArgument("conversationId", 9813)
                    .AddToastActivationInfo(null, ToastActivationType.Foreground)
                    .AddText(title, hintStyle: AdaptiveTextStyle.Header)
                    .AddText(message, hintStyle: AdaptiveTextStyle.Body)
                    .SetBackgroundActivation()
                    .Schedule((DateTimeOffset)notifyTime.Value, t =>
                    {
                        t.ExpirationTime = notifyTime.Value.AddDays(1);
                        t.Tag = _tag_ID;
                        t.Group = Utility.Appl.AppName;
                    });

(Of course, the number “9813” is then a variable.)

If I then want to read the argument when opening the app according to gurrenm3 and his instructions, then this must happen in the Platforms\Windows\ App.xaml.cs like this:

public partial class App : MauiWinUIApplication
{
    public Action<string> ShowPopup { get; set; }

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();

        // need to add this because otherwise setting background activation does nothing.
        ToastNotificationManagerCompat.OnActivated += (notificationArgs) =>
        {
            // this will run everytime ToastNotification.Activated is called,
            // regardless of what toast is clicked and what element is clicked on.
            // Works for all types of ToastActivationType so long as the Windows app manifest
            // has been updated to support ToastNotifications. 

            // you can check your args here, however I'll be doing mine below to keep it cleaner.
            // With so many ToastNotifications it would be messy to check all of them here.

            Debug.WriteLine($"A ToastNotification was just activated! Arguments: {notificationArgs.Argument}");

            // using the code below to show a popup from MainPage, saying that the toast itself was clicked.
            if (notificationArgs.Argument.Contains("action=toastClicked"))
                ShowPopup?.Invoke("The Toast was clicked!");
        };
    }

The problem here is that this code is not run at all when the app is opened from the notification:

ToastNotificationManagerCompat.OnActivated += (notificationArgs) =>
{
      // you don't need to put anything here if you don't want to. 
      // You just need to subscribe to it in your app
};

I then checked the Microsoft documentation step by step. The first part of the documentation is sending the notification and that works:

https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/adaptive-interactive-toasts?tabs=appsdk

The second part of the documentation then describes in a very short section how to read the parameters after clicking on the notification in the app. The description is given in three variants:

https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=uwp

Here is the most important step 3 (Step 3: Handling activation) with these three options: UWP, Desktop(MSIX) and Desktop (unpackaged). If I want to implement the UWP variant, then the “OnActivated” event is unknown under MAUI. The same applies to the third variant with OnStartup and OnActivated events. Apparently this description refers to WPF apps and not to MAUI.

Ahhh almost forgot, I also asked all AIs for a solution, but only there I don't get anything concrete (probably the data is missing). In forums you can only find this one post, from gurrenm3.

I didn't know what else to try? Or is it because of MAUI that it is not possible to read notification arguments under MAUI? Check please also #5945 .

I would be very grateful for any information.

Regards
pc

Steps to Reproduce

  1. take the project from https://github.com/gurrenm3/Toast-Notifications-with-.Net-Maui
  2. try to read an argument from the notification when the user clicks on the notification (your app is closed and should be open)
  3. if your app is now open by notification, the argument from the notification will not be read.

Link to public reproduction project repository

https://github.com/gurrenm3/Toast-Notifications-with-.Net-Maui

Version with bug

9.0.0-rc.1.24453.9

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

8.0.82 SR8.2

Affected platforms

Windows

Affected platform versions

Windows 11

Did you find any workaround?

No

Relevant log output

No logs
@true-perfect-code true-perfect-code added the t/bug Something isn't working label Sep 16, 2024
Copy link
Contributor

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@tj-devel709 tj-devel709 added this to the Backlog milestone Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform/windows 🪟 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants