Toast Notification in Windows Phone 8.1 API

Introduction

 
So, we have moved to Windows Phone 8.1 and with this new version, the API has introduced a Notification Bar. In the early API, the major features were Toast Notification and Push Notification.
 
For now, in this article, we will implement a small demonstration over the Notification Bar.
 
 

Procedures

 
Step 1
 
Create a Blank Windows Phone Project with Windows Phone 8.1 API.
 
 
And add a button that will show the notification.
 
Step 2
 
Now, raise the Click event of the button and add the following code in the event handler.
 
<code>
  1. <code>    
  2. private void simpleToast_Click(object sender, RoutedEventArgs e)    
  3.         {    
  4.             ToastTemplateType toastType = ToastTemplateType.ToastText02;    
  5.     
  6.             XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastType);    
  7.     
  8.             XmlNodeList toastTextElement = toastXml.GetElementsByTagName("text");    
  9.             toastTextElement[0].AppendChild(toastXml.CreateTextNode("Hello C# Corner"));    
  10.             toastTextElement[1].AppendChild(toastXml.CreateTextNode("I am poping you from a Winmdows Phone App"));    
  11.     
  12.             IXmlNode toastNodetoastXml.SelectSingleNode("/toast");    
  13.             ((XmlElement)toastNode).SetAttribute("duration","long");    
  14.     
  15.             ToastNotification toast = new ToastNotification(toastXml);    
  16.             ToastNotificationManager.CreateToastNotifier().Show(toast);    
  17.     
  18.     
  19.         }    
  20. </code>    
Before you start, ensure you have the following namespace in your project.
  1. using Windows.UI.Notifications;  
  2. using Windows.Data.Xml.Dom; 
Output
 
 
Note
 
You must have Visual Studio 2013 Update 2 at least with the Windows Phone 8.1 Emulator or any supported device to run this demo app.
 

Conclusion

 
It's a small demonstration of Toast Notification that we have implemented with the help of the Windows Phone 8.1 API.


Similar Articles