Implement Local Tile Notification In Windows 10 Universal App

A tile begins as a default tile, which is defined in your application manifest file for a primary tile, and you can send programmatically to display notifications on your tile. A local notification means that the notification is sent from the app’s code, rather than being pushed or pulled from a web server.

Let’s see the steps to send local primary tile notification in Windows 10 universal app using adaptive tile templates.

Firstly, we need to install NotificationsExtensions.Win10 NuGet package like the following screen. It will generate tile payloads via objects instead of creating raw XML.

package

Then add the following namespace to access the notification classes.

  1. using Windows.UI.Notifications;  
  2. using NotificationsExtensions.Tiles;  
Next create three string for tile from, subject and body.
  1. string from = "Suresh";  
  2. string subject = "Tile Notification";  
  3. string body = "Tile notification sample for windows 10";  
Next Create notification content. In windows 10 we can create custom visual layout for tile notification. Write the following code to create the content for tile notification.

Here I create one method to create the content.
  1. public void createContent()  
  2. {  
  3. TileContent conetn = new TileContent()  
  4. {  
  5. Visual = new TileVisual()  
  6. {  
  7. TileSmall = new TileBinding()  
  8. {  
  9. Content = new TileBindingContentAdaptive()  
  10. {  
  11. Children ={  
  12. new TileText()  
  13. {  
  14. Text = from,  
  15. Style=TileTextStyle.CaptionSubtle  
  16. },  
  17. new TileText()  
  18. {  
  19. Text = subject,  
  20. Style = TileTextStyle.Subtitle  
  21. },  
  22. new TileText()  
  23. {  
  24. Text = body,  
  25. Style = TileTextStyle.Body  
  26. }  
  27. }  
  28. }  
  29. },  
  30. TileMedium = new TileBinding()  
  31. {  
  32. Content = new TileBindingContentAdaptive()  
  33. {  
  34. Children ={  
  35. new TileText()  
  36. {  
  37. Text = from  
  38. },  
  39. new TileText()  
  40. {  
  41. Text = subject,  
  42. Style = TileTextStyle.CaptionSubtle  
  43. },  
  44. new TileText()  
  45. {  
  46. Text = body,  
  47. Style = TileTextStyle.CaptionSubtle  
  48. }  
  49. }  
  50. }  
  51. },  
  52.   
  53. TileWide = new TileBinding()  
  54. {  
  55. Content = new TileBindingContentAdaptive()  
  56. {  
  57. Children =  
  58. {  
  59. new TileText()  
  60. {  
  61. Text = from,  
  62. Style = TileTextStyle.Subtitle  
  63. },  
  64. new TileText()  
  65. {  
  66. Text = subject,  
  67. Style = TileTextStyle.CaptionSubtle  
  68. },  
  69. new TileText()  
  70. {  
  71. Text = body,  
  72. Style = TileTextStyle.CaptionSubtle  
  73. }  
  74. }  
  75. }  
  76. }  
  77. }  
  78. };  
  79.   
  80. var notification = new TileNotification(conetn.GetXml());  
  81. TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);  
  82. }  
We need to define the small large and medium size tile notification, you can set your font style also. Once you created your notification content you need to create the tile notification using TileNotification method by passing the notification content.

Finally you need to send the notification by using TileUpdateManager.

Optional features 
  • Set an expiration time for tile notification
  • Clear tile notifications

Set an expiration time for tile notification

By default local tile notifications do not expire for that we can set time to remove the notification by using the following code.

  1. notification.ExpirationTime = DateTimeOffset.UtcNow.AddMinutes(5);  
Clear tile notifications

When the user interacts with  the application we need to clear all the notifications from the tile; for that write the following code.
  1. TileUpdateManager.CreateTileUpdaterForApplication().Clear();  
Now run the app and check the output like the following image.

app

Read more articles on Windows 10:


Similar Articles