Toast Notifications

Visual Basic tutorials for UWP apps.
6 posts Page 1 of 1
Contributors
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Toast Notifications
CodenStuff
How to show toast notifications

A toast notification is basically a popup that appears in the top-right corner of the screen to inform the user of activity within your app. An email app for example would use these notifications to let the user know they have a new email or an IM app could use it to let you know when someone logs on or sends you a message.

You can use 4 different types of toast notifications with or without an image and below you will find the basic codes on how to use them all.

Image

ToastText01
Displays a single string wrapped across 3 lines.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText01)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Single line of text wrapped across 3 lines inside the popup notification."))
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastText02
Display first string in bold and a second in regular wrapped across 2 lines.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("Single line of text wrapped across 2 lines inside the notification popup."))
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastText03
Display first string in bold wrapped across 2 lines and a second in regular on the third line.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText03)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title wrapped across 2 lines in the notification"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("Single line of text"))
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastText04
Display first string in bold a second string in regular and a third string in regular.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText04)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("First line of text"))
        ToastXML.GetElementsByTagName("text").ElementAt(2).AppendChild(ToastXML.CreateTextNode("Second line of text"))
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))

ToastImageAndText01
Image with single string wrapped across 3 lines.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText01)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Single line of text wrapped across 3 lines inside the popup notification."))
        Dim imageElements As XmlNodeList = ToastXML.GetElementsByTagName("image")
        DirectCast(imageElements.ElementAt(0), XmlElement).SetAttribute("src", "ms-resource:images/class_scoutred.png")
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastImageAndText02
Image with first string in bold and second regular wrapped across 2 lines.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText02)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("Single line of text wrapped across 2 lines inside the notification popup."))
        Dim imageElements As XmlNodeList = ToastXML.GetElementsByTagName("image")
        DirectCast(imageElements.ElementAt(0), XmlElement).SetAttribute("src", "ms-resource:images/class_scoutred.png")
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastImageAndText03
Image with first string in bold across 2 lines and second string regular on third line.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText03)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title wrapped across 2 lines in the notification"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("Single line of text"))
        Dim imageElements As XmlNodeList = ToastXML.GetElementsByTagName("image")
        DirectCast(imageElements.ElementAt(0), XmlElement).SetAttribute("src", "ms-resource:images/class_scoutred.png")
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
ToastImageAndText04
Image with first string in bold a second string in regular and a third string in regular.
Code: Select all
Dim ToastXML As XmlDocument = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastImageAndText04)
        ToastXML.GetElementsByTagName("text").First().AppendChild(ToastXML.CreateTextNode("Toast Title"))
        ToastXML.GetElementsByTagName("text").ElementAt(1).AppendChild(ToastXML.CreateTextNode("First line of text"))
        ToastXML.GetElementsByTagName("text").ElementAt(2).AppendChild(ToastXML.CreateTextNode("Second line of text"))
        Dim imageElements As XmlNodeList = ToastXML.GetElementsByTagName("image")
        DirectCast(imageElements.ElementAt(0), XmlElement).SetAttribute("src", "ms-resource:images/class_scoutred.png")
        ToastNotificationManager.CreateToastNotifier().Show(New ToastNotification(ToastXML))
In the ToastImageAndText notifications you will want to replace "ms-resource:images/class_scoutred.png" with your own image resource/url you wish to display with your toast notification.

You need to change the "Toast Capable" setting in your app package settings to 'Yes' in order to use toast within your app.

Hope they come in handy cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Toast Notifications
Usman55
Is this for Windows 8 only? Because I remember using completely different coding in my softwares.
Image
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Toast Notifications
CodenStuff
Yes its for W8...it is posted in the W8 section lol

This is all metro code like the others ive been posting which will hopefully help people with the basics when they move to metro programming and im still learning myself but my brain is having a hard time with all this new stuff and xaml gives me a headache :lol:
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Toast Notifications
Shim
just now and from this post only i heard XAML

thanks codenstuff , i will be trying to learn xaml now

:D :D
Find my programs on Softpedia
User avatar
Usman55
VIP - Site Partner
VIP - Site Partner
Posts: 2821
Joined: Thu Dec 24, 2009 7:52 pm

Re: Toast Notifications
Usman55
CodenStuff wrote:
Yes its for W8...it is posted in the W8 section lol

This is all metro code like the others ive been posting which will hopefully help people with the basics when they move to metro programming and im still learning myself but my brain is having a hard time with all this new stuff and xaml gives me a headache :lol:
Actually, I only saw the 'Visual Basic' at the end and got confused. I didn't read the whole directory.
Image
User avatar
MrAlicard
VIP - Donator
VIP - Donator
Posts: 54
Joined: Thu Aug 05, 2010 4:08 pm

Re: Toast Notifications
MrAlicard
:O
Coooool source. :D
You are best. Thanks boss. :)
6 posts Page 1 of 1
Return to “Visual Basic”