3

I am trying to implement Toast Notification from Console App in C# using the below code.

I also tried the wpf code, and it works just fine.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications; // Notifications library
using Windows.Data.Xml.Dom;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            // In a real app, these would be initialized with actual data
            string title = "Andrew sent you a picture";
            string content = "Check this out, Happy Canyon in Utah!";
            string image = "http://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-71-81-permanent/2727.happycanyon1_5B00_1_5D00_.jpg";
            string logo = "ms-appdata:///local/Andrew.jpg";
            int conversationId = 384928;

            // Construct the visuals of the toast
            ToastVisual visual = new ToastVisual()
            {
                BindingGeneric = new ToastBindingGeneric()
                {
                    Children =
                    {
                        new AdaptiveText()
                        {
                            Text = title
                        },

                        new AdaptiveText()
                        {
                            Text = content
                        },

                        new AdaptiveImage()
                        {
                            Source = image
                        }
                    },

                    AppLogoOverride = new ToastGenericAppLogo()
                    {
                        Source = logo,
                        HintCrop = ToastGenericAppLogoCrop.Circle
                    }
                }
            };

            // Construct the actions for the toast (inputs and buttons)
            ToastActionsCustom actions = new ToastActionsCustom()
            {
                Inputs =
                {
                    new ToastTextBox("tbReply")
                    {
                        PlaceholderContent = "Type a response"
                    }
                }

            };


            // Now we can construct the final toast content
            ToastContent toastContent = new ToastContent()
            {
                Visual = visual,
                Actions = actions,

                // Arguments when the user taps body of toast

            };
            //Console.WriteLine(toastContent.GetContent());
            //Console.ReadKey();
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(toastContent.GetContent());


            // And create the toast notification
            ToastNotification notification = new ToastNotification(xDoc);


            // And then send the toast
            ToastNotificationManager.CreateToastNotifier("Test").Show(notification);
        }
    }
}

I dont see any errrors in the code, but for some reason the Toast is not showing up.

The same code works perfectly with WPF app.

Can anyone help me what i am missing here?

1 Answers1

2

I also ran into the same issue with a Console Application where I used sample code from the Notifications Visualizer app. This code did not have the applicationId string and crashed, exacly as described in this question.

But after adding an applicationId, nothing happened. There were no compliation errors and no exceptions were raised. It turns out you now have to use the ToastNotificationManagerCompat class instead of ToastNotificationManager.

Replace:

// And then send the toast
ToastNotificationManager.CreateToastNotifier("Test").Show(notification);

With:

// And then send the toast
ToastNotificationManagerCompat.CreateToastNotifier().Show(notification);
jvpernis
  • 195
  • 8