The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[UWP][C++]How to make use ToastNotification in C++/CX specifically? RRS feed

  • Question

  • Hi MSDN,

    I am developing an UWP timer app. I want to show a ToastNotification after the timer has been done. I use C++/CX to develop. Could anyone tell me the specific ways to create a ToastNotification with

    1. text "Time's up!"
    2. an button "Dismiss"

    When the button is clicked, the app calls an function I created. For example, the function's name is OnTimerFinish().

    I think the documentations on MSDN are too difficult to understand since "ToastNotification" page's examples are written in JavaScript, however I use C++/CX.

    Thanks.

    Leo Liang 


    Windows 10: One Windows!



    Saturday, January 30, 2016 2:54 AM

Answers

  • To create the notification:

    void ToastInCpp::MainPage::ShowToast()
    {
    	auto toastXml = ref new XmlDocument();
    	toastXml->LoadXml("<toast>"
    		"<visual>"
    		"<binding template = 'ToastGeneric'>"
    		"<text>Ghost Toast</text>"
    		"<text>Time's up</text>"
    		"</binding>"
    		"</visual>"
    		"<actions>"
    		"<action content = 'dismiss' activationType='foreground' arguments = 'dismiss'/>"
    		"</actions>"
    		"</toast>");
    
    	auto toast = ref new ToastNotification(toastXml);
    	ToastNotificationManager::CreateToastNotifier()->Show(toast);
    }
    

    Then you can catch the 'dismiss' action inside your App::Onactivated method:

    void App::OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs^ e)
    {
    	auto ev = dynamic_cast<ToastNotificationActivatedEventArgs^>(e);
    
    	if (ev)
    	{
    		auto arg = ev->Argument;
    		if (arg == "dismiss")
    		{
    			// Stop the timer...
    		}
    	}
    }
    


    http://peted.azurewebsites.net/

    • Marked as answer by Liangyiliang Tuesday, February 2, 2016 3:19 AM
    Monday, February 1, 2016 9:35 PM

All replies

  • Hello,

    Welcome to the Developing Universal Windows apps forum!

    As a friendly reminder please make sure to add the appropriate tags to the title of your post as per Guide to posting: subject line tags

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best Regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, February 1, 2016 12:13 PM
    Owner
  • To create the notification:

    void ToastInCpp::MainPage::ShowToast()
    {
    	auto toastXml = ref new XmlDocument();
    	toastXml->LoadXml("<toast>"
    		"<visual>"
    		"<binding template = 'ToastGeneric'>"
    		"<text>Ghost Toast</text>"
    		"<text>Time's up</text>"
    		"</binding>"
    		"</visual>"
    		"<actions>"
    		"<action content = 'dismiss' activationType='foreground' arguments = 'dismiss'/>"
    		"</actions>"
    		"</toast>");
    
    	auto toast = ref new ToastNotification(toastXml);
    	ToastNotificationManager::CreateToastNotifier()->Show(toast);
    }
    

    Then you can catch the 'dismiss' action inside your App::Onactivated method:

    void App::OnActivated(Windows::ApplicationModel::Activation::IActivatedEventArgs^ e)
    {
    	auto ev = dynamic_cast<ToastNotificationActivatedEventArgs^>(e);
    
    	if (ev)
    	{
    		auto arg = ev->Argument;
    		if (arg == "dismiss")
    		{
    			// Stop the timer...
    		}
    	}
    }
    


    http://peted.azurewebsites.net/

    • Marked as answer by Liangyiliang Tuesday, February 2, 2016 3:19 AM
    Monday, February 1, 2016 9:35 PM
  • Hey Leo,

    Sounds like you're looking at old documentation. See our Quickstart for sending a local toast notification, and also our adaptive/interactive toast notification documentation. They're both in C#.

    You can use NotificationsExtensions.Win10 to generate toast notification content without dealing with the messy XML. Or you can use raw XML like Baba proposed, but it's more prone to errors.

    NotificationsExtensions.Win10 is written as a WinRT component, so you can use it from C++ in addition to C#. You can install it via NuGet, or click the link that I provided earlier.

    Hope that helps!
    Andrew

    • Proposed as answer by andrewbares7 Tuesday, February 2, 2016 9:24 PM
    Tuesday, February 2, 2016 9:24 PM