Page 1 of 1

Toast Notifications

Posted: Tue May 08, 2012 3:13 am
by 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;

Re: Toast Notifications

Posted: Tue May 08, 2012 8:02 am
by Usman55
Is this for Windows 8 only? Because I remember using completely different coding in my softwares.

Re: Toast Notifications

Posted: Tue May 08, 2012 9:00 am
by 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:

Re: Toast Notifications

Posted: Tue May 08, 2012 9:36 am
by Shim
just now and from this post only i heard XAML

thanks codenstuff , i will be trying to learn xaml now

:D :D

Re: Toast Notifications

Posted: Tue May 08, 2012 9:40 am
by 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.

Re: Toast Notifications

Posted: Sun Jul 01, 2012 7:22 pm
by MrAlicard
:O
Coooool source. :D
You are best. Thanks boss. :)