WinRT JavaScript - Toast Notification |
Written by Ian Elliot | ||||
Page 3 of 3
Activate!A toast can be used to simply supply some information to the user but most often it is alerting the user to something happening in your app. If the user taps or clicks on the toast then your app can intercept an activate event that occurs and can do something in response. How this works is very simple indeed as it really is only standard event handling.First we need to define a function to handle the event:
In this case the event hander simply stores the type of the event which should be "activate" in this case. Next we need to subscribe to the event:
Now if you run the program you will discover that clicking or tapping on the toast calls the event handler. You can also subscribe to a "dismissed" and a "failed" event that occur then the toast is dismissed (or simply times out) or if the toast failed for some reason., In the case of activation you often need some additional information about the toast that had caused the activation. You can store information in the ToastNotification object that will be passed to the event handler. To do this you need to set the launch attribute of the toast tag in the XML to a string - usually corresponding to a JSON format object - that provides the data. For example:
To retrieve the string you would use something like
Scheduled ToastsAs in the case of tile notificiations you can schedule a toast notification. To do this you have to create a ScheduledToastNotification object. This has a startTime parameter which determines when the toast will be displayed. You can also specify a repeat time to create recuring toasts. After you have create the object you add it to the ToastNotifier's queue using the addToSchedule method. If you think about the way toasts are supposed to be used then the idea of a scheduled toast doesn't make much sense and a recurring toast makes even less sense. In addition a scheduled toast doesn't raise any events so you can use it to reactiveate your app. The only use for a scheudled toast is to provide information to the user that something is still alive and happening or to deliver advertising. For example, to create a toast that shows after 3 seconds:
The id attribute has to be 16 characters in length and it can be used to cancel a scheduled toast. More ToastAs well as simple toasts generated from your app you can also create toasts that are pushed from a WNS server. This is beyond the scope of this chapter. You can also send toasts from desktop apps, but as these cannot be written in JavaScript this topic isn't covered.
Creating JavaScript/HTML5 Metro Applications
Coming soon, chapters on Promises, Life Cycle, Custom Controls and Data Binding.
Comments
or email your comment to: comments@i-programmer.info
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter. |