Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display local toast notification

I want to develop a universal app for Windows Phone 8.1 which contains a local “Notification”.

What I want to do is to show all messages to the user (error, information, warnings) in a kink of toast control. Everything is done locally without going through the standard notification system. There are several system that work on Windows Phone 8:

  • TOASTINET (http://www.nuget.org/packages/ToastinetWP/)
  • CONDING4FUN toast prompt (https://coding4fun.codeplex.com/wikipage?title=Toast%20Prompt&referringTitle=Documentation

But it's not possible to include those libraries on windows phone 8.1 project.

Does anyone know another method to display "local" toasts?

like image 709
Geoffrey Lalloué Avatar asked May 06 '14 14:05

Geoffrey Lalloué


People also ask

How do I turn on toast notifications?

Set Up NotificationsFrom the Kitchen Setup page of the Toast Web, select Notification Setup from the POS notifications section. Enabled Notifications will appear with a blue checkmark. To disable a notification, uncheck the notification. (Save and Publish for change to take effect.)

What is a Windows toast notification?

A toast notification is a message that your app can construct and deliver to your user while they are not currently inside your app. This quickstart walks you through the steps to create, deliver, and display a Windows 10 or Windows 11 toast notification using rich content and interactive actions.

Where should toast messages appear?

Toast messages ensure that the use of the application is not interrupted while providing necessary information for the user. They have no notification sounds associated with them and don't appear in the notification centers on any platform, but appear at the bottom of the viewport by default.

What is the difference between toast and notification?

A toast is a small display on the bottom of the screen. A notification is displayed in the top menu bar. Save this answer.


2 Answers

With the help of the @msimons response and the following url : http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868254.aspx I succeed to display my notifications.

For those who need it, here is my final method :

private void ShowToastNotification(String message)
    {
        ToastTemplateType toastTemplate = ToastTemplateType.ToastImageAndText01;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);

        // Set Text
        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
        toastTextElements[0].AppendChild(toastXml.CreateTextNode(message));

        // Set image
        // Images must be less than 200 KB in size and smaller than 1024 x 1024 pixels.
        XmlNodeList toastImageAttributes = toastXml.GetElementsByTagName("image");
        ((XmlElement)toastImageAttributes[0]).SetAttribute("src", "ms-appx:///Images/logo-80px-80px.png");
        ((XmlElement)toastImageAttributes[0]).SetAttribute("alt", "logo");

        // toast duration
        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        ((XmlElement)toastNode).SetAttribute("duration", "short");

        // toast navigation
        var toastNavigationUriString = "#/MainPage.xaml?param1=12345";
        var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
        toastElement.SetAttribute("launch", toastNavigationUriString);

        // Create the toast notification based on the XML content you've specified.
        ToastNotification toast = new ToastNotification(toastXml);

        // Send your toast notification.
        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

I tested on a universal application windows phone 8.1.

And don't forget to edit "Package.appxmanifest" and activate notifications. The capability to raise toast notifications is declared in your app's package.appxmanifest file. If you use the Microsoft Visual Studio manifest editor, simply set the Toast capable option to "Yes" in the Notification section of the Application tab.

like image 160
Geoffrey Lalloué Avatar answered Oct 30 '22 13:10

Geoffrey Lalloué


You could use a local notification that appears when your app is running.

ToastTemplateType toastTemplateXml = ToastTemplateType.ToastImageAndText01; 
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplateXml);

You'll then need to populate the XML returned by GetTemplateContent

<toast>
    <visual>
        <binding template="ToastImageAndText01">
            <image id="img" src=""/>
            <text id="txt"></text>
        </binding>
    </visual>
</toast>

Supply the content of your toast in the XML DOM. The image is relevant only for Windows 8.1.

Specify it's launch parameters

((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"1\",\"param2\":\"2\"}");

Create the toast object:

ToastNotification toast = new ToastNotification(toastXml);

and finally display the toast.

ToastNotificationManager.CreateToastNotifier().Show(toast);

Also, If you want to use a third party control to display the toast then you could consider writing a Windows Phone 8.1 Silverlight app.

like image 26
msimons Avatar answered Oct 30 '22 12:10

msimons